[英]Why do I see '\n' seen when reading from a text file? (python)
text_file = open("Accounts.txt")
Accounts = text_file.read().split(',').strip
I'm reading a text file using this and splitting it into a list however, when I print this list the output has the desired values with '\n' in it.我正在使用它读取文本文件并将其拆分为列表,但是,当我打印此列表时,output 具有所需的值,其中包含“\n”。 However, when opening the text file I dont see this string但是,当打开文本文件时,我没有看到这个字符串
f = open("Accounts.txt",'a')
f.write(Combined)
f.write('\n')
f.close()
This was how I was writing to the text file.这就是我写入文本文件的方式。
Text files have a newline character in them which is different depending on whether you are on windows or linux.文本文件中有一个换行符,这取决于您使用的是 windows 还是 linux。
Since you didn't say which system you're on, I'm assuming that's what's going on here.既然你没有说你在哪个系统上,我假设这就是这里发生的事情。 You should also read text files in binary mode generally.您通常还应该以二进制模式阅读文本文件。 I would also recommend using the with statement to close is called automatically.我还建议使用 with 语句来自动调用 close。
with open("Accounts.txt",'ab') as f:
f.write(Combined)
f.write('\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.