【发布时间】:2020-07-26 07:09:22
【问题描述】:
我试过这样做:
with open("contactbook.txt","a") as f:
f.write("hello\n")
但它被打印为:
'hello\n'
我读取文件的代码:
with open("contactbook.txt","r") as f:
lines = sorted(f.readlines())
print(lines)
编辑:
我试图检查文本文件,其中 \n 确实被解释为换行符!
但在 python 的 shell 中它仍然显示为 \n
我做错了什么?是因为我的打印方式吗?
【问题讨论】:
-
你用什么程序来检查它是如何打印的?
-
嘿,这是完美的工作,没有错,删除文件并再次运行。这可能是由于附加模式,您可能会再次看到旧数据。
-
此代码应按需要运行。你在什么操作系统上工作?您使用的是什么 IDE/编辑器?
-
@VigneshRajendran 你好!我已经这样做了,但它仍然打印为 \n :(
-
@AlbertAlberto Windows 10,python 3.8.3