有没有办法逃脱python中的任何角色

发布于 2025-01-24 04:52:43 字数 329 浏览 0 评论 0原文

因此,我正在进行一个项目,我必须手动存储数据(必须可读) 我正在使用“ |”作为定界线。因此,显然,当我遇到'|'时要输入文件中的符号,我必须采取一种逃避阅读的方式,以防止错误的读数。

但是,与默认的逃生字符不同,例如\ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-01-31 04:52:43

它只是以这种方式打印。如果您执行print(r'\ |'),它将打印\ |。尝试将其写入这样的文件:

with open(filename, 'w') as f:
    f.write(r'\|')

这将把\ |放入文件中。

It's just being printed that way. If you do print(r'\|'), it will print \|. Try writing it to a file like this:

with open(filename, 'w') as f:
    f.write(r'\|')

This will put \| into the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文