有没有办法逃脱python中的任何角色
因此,我正在进行一个项目,我必须手动存储数据(必须可读) 我正在使用“ |”作为定界线。因此,显然,当我遇到'|'时要输入文件中的符号,我必须采取一种逃避阅读的方式,以防止错误的读数。
但是,与默认的逃生字符不同,例如\ n或\ t
,\ |
存储在变量中时,会自动更改为\\ |
。我尝试了原始字符串,使用替换功能
等
>>>x = '\|'
>>>x
'\\|'
>>>x = '\t'
>>>x
'\t'
So Im doing a project, where I have to store data in a file manually(must be readable)
Im using the '|' as a delimiter. So obviously, when i encounter a '|' symbol to be inputted into the file, I must make a way of escaping reading that to prevent a wrong read.
But unlike default escape characters, like \n or \t
, \|
when stored in a variable automatically changes to \\|
. I have tried raw strings, using replace function, etc.
But none helpful
>>>x = '\|'
>>>x
'\\|'
>>>x = '\t'
>>>x
'\t'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它只是以这种方式打印。如果您执行
print(r'\ |')
,它将打印\ |
。尝试将其写入这样的文件:这将把
\ |
放入文件中。It's just being printed that way. If you do
print(r'\|')
, it will print\|
. Try writing it to a file like this:This will put
\|
into the file.