我正在读取一个包含以下内容的文件:
this is the first line\n this is the second line\n\nAnother line
我试图读取这个文件并保留特殊字符\n
不变。
with open(r'test.txt') as f:
c = f.read()
但是打印c
总是显示:
this is the first line\\n this is the second line\\n\\nAnother line
我尝试了没有在r'text.txt'
中加前缀r
,但是它仍然没有改变任何内容。
是否有可能避免对特殊字符\n
进行转义?
当然,我可以使用str.replace('\\n','\n')
,但我只是想知道是否能够省略这一步骤。
c = f.readlines()
- roganjoshimport re; re.sub('\s+', ' ',c)
。 - BoyInDaBox89c = f.readlines()
,但它仍然无法转义。 - ThePhi