Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在windows上读取文件时,结果字符串中的回车字符丢失_Python_Windows_Io - Fatal编程技术网

Python 在windows上读取文件时,结果字符串中的回车字符丢失

Python 在windows上读取文件时,结果字符串中的回车字符丢失,python,windows,io,Python,Windows,Io,用python在windows中读取txt文件时,结果字符串中的回车符丢失 c:/text.txt aaa\r\nbbb\r\nccc\r\nddd 代码: 结果: 0 97 'a' 1 97 'a' 2 97 'a' 3 10 '\n' 4 98 'b' 5 98 'b' 6 98 'b' 7 10 '\n' 8 99 'c' 9 99 'c' 10 99 'c' 11 10 '\n' 12 100 'd' 13 100 'd' 14 100 'd' 您可以看到所有的回车字符都丢失了。 如

用python在windows中读取txt文件时,结果字符串中的回车符丢失

c:/text.txt

aaa\r\nbbb\r\nccc\r\nddd
代码:

结果:

0 97 'a'
1 97 'a'
2 97 'a'
3 10 '\n'
4 98 'b'
5 98 'b'
6 98 'b'
7 10 '\n'
8 99 'c'
9 99 'c'
10 99 'c'
11 10 '\n'
12 100 'd'
13 100 'd'
14 100 'd'
您可以看到所有的回车字符都丢失了。 如有任何建议,我们将不胜感激


谢谢。

如果以文本模式打开文件,Windows行尾
\r\n
将自动替换为标准行尾
\n
。要防止这种情况发生,请以二进制模式打开文件:

input = open('c:/text.txt', 'rb')

如果以文本模式打开文件,Windows行尾
\r\n
将自动替换为标准行尾
\n
。要防止这种情况发生,请以二进制模式打开文件:

input = open('c:/text.txt', 'rb')

-不,他错了。Python 2.7说
ValueError:mode字符串必须以“r”、“w”、“a”或“U”中的一个开头,而不是“b”
@John:谢谢你指出这一点。我真的要小心点@乔恩:谢谢。我几乎忽略了-不,他错了。Python 2.7说
ValueError:mode字符串必须以“r”、“w”、“a”或“U”中的一个开头,而不是“b”
@John:谢谢你指出这一点。我真的要小心点@乔恩:谢谢。我几乎忽略了这一点。