问题描述
我有以下文件:
abcde
kwakwa
<0x1A>
line3
linllll
其中<0x1A>
代表十六进制值为0x1A的字节.尝试以以下方式在Python中读取此文件时:
Where <0x1A>
represents a byte with the hex value of 0x1A. When attempting to read this file in Python as:
for line in open('t.txt'):
print line,
它仅读取前两行,并退出循环.
It only reads the first two lines, and exits the loop.
解决方案似乎是用二进制(或通用换行模式)-'rb'或'rU'打开文件.您能解释这种行为吗?
The solution seems to be to open the file in binary (or universal newline mode) - 'rb' or 'rU'. Can you explain this behavior ?
推荐答案
0x1A是Ctrl-Z,而DOS在历史上一直将其用作文件结尾标记.例如,尝试使用命令提示符,然后键入"文件.它将仅在Ctrl-Z上显示内容.
0x1A is Ctrl-Z, and DOS historically used that as an end-of-file marker. For example, try using a command prompt, and "type"ing your file. It will only display the content up the Ctrl-Z.
Python使用Windows CRT函数_wfopen,该函数实现了"Ctrl-Z为EOF"语义.
Python uses the Windows CRT function _wfopen, which implements the "Ctrl-Z is EOF" semantics.
这篇关于0x1A上的线路读取扼流圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!