简体   繁体   English

为什么从文本文件中读取时看到“\n”? (Python)

[英]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.

相关问题 当我从 Python 中的 utf-8 文件打印文本时,为什么看不到希伯来语字符? - Why don't I see the hebrew characters, when I print text from an utf-8 file in Python? 从文件中读取文本时,如何从 escaping 停止 Python 反斜杠? - How do I stop Python from escaping the backslashes when reading text from a file? 使用python从文本文件末尾读取n行 - Reading n lines from end of text file using python 通过Python读取文本文件时如何保持文本文件的格式? - How do I maintain the format of a text file when reading a text file through Python? 从文件python读取时的不同文本 - Different text when reading from file python 为何在读取文件时包含\\ n? 蟒蛇 - Why is \n included when reading files? Python 读取文本文件时,为什么在每行前面都出现b'? - When reading a text file, why do I get b' in front of each line? 为什么我的 Python 代码在读取文本文件时会打印额外的字符“”? - Why does my Python code print the extra characters "" when reading from a text file? 读取文件时删除python中的'\\ n'符号 - Discarding the '\n' symbol when reading a file, python 如何使用sed -n在Python中从文本文件中提取一系列行? - How do I extract a range of lines from a text file using sed -n but in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM