[英]Wrong readline() in Python
我有一個readline()方法的問題,它有時返回2行而不是一行,我不知道為什么。 有人能幫我嗎 ?
這里是我讀過的文本文件的一部分(帶記事本):
at+gpsinit=2
OK
+GPSEVINIT: 1
at+gpsnmea=3
OK
at+gpsstart=0
OK
並使用Notepad ++:
at+gpsinit=2CR
CR LF
OKCR LF
CR LF
+GPSEVINIT: 1CR LF
at+gpsnmea=3CR
CR LF
OKCR LF
at+gpsstart=0CR
CR LF
OKCR LF
這是我在Python shell中得到的:
16 : at+gpsinit=2
17 :
18 : OK
19 :
20 : +GPSEVINIT: 1
21 : at+gpsnmea=3
在這里我的代碼:
# Open a file
file = open("testtxt.txt", 'r')
line = 0
for current_line in file:
line += 1
print(str(line)+" : "+current_line)
# Close opend file
file.close()
您遇到的問題很可能是由於行尾標記出現問題。
CRLF
(或, \\r\\n
,或以字節為單位的0d0a
)。 LF
(或\\n
,或字節為0a
) CR
(或\\r
或字節為0d
) 以下是ASCII編碼文件的一些示例:
$ hexdump -C test_dos.txt
00000000 68 65 6c 6c 6f 0d 0a 77 6f 72 6c 64 0d 0a |hello..world..|
0000000e
$ hexdump -C test_nix.txt
00000000 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a |hello.world.|
0000000c
$ hexdump -C test_mac.txt
00000000 68 65 6c 6c 6f 0d 77 6f 72 6c 64 0d |hello.world.|
0000000c
廣告你可以看到,單詞hello
( 68 65 6c 6c 6f
)后面跟着不同的字節0d 0a
, 0a
或0d
。 在MS-Notepad中編輯文件時,很可能會插入CRLF
。 由於LF
在軟件開發中最常見,因此Notepad ++最有可能添加這些。
現在,對於您的代碼:鑒於上面的三個文件,與您類似的代碼產生以下結果:
碼:
files = ('test_dos.txt', 'test_nix.txt', 'test_mac.txt')
for fname in files:
print("Reading {}".format(fname))
with open(fname) as fptr:
for line in fptr:
print("--> {!r}".format(line))
print(80*"-")
輸出:
Reading test_dos.txt
--> 'hello\r\n'
--> 'world\r\n'
--------------------------------------------------------------------------------
Reading test_nix.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------
Reading test_mac.txt
--> 'hello\rworld\r'
--------------------------------------------------------------------------------
正如您可以清楚地看到的,Python在\\n
字符上分割,但不會從輸出中刪除它。 這就是為什么“mac”示例只有一行。
如果您必須處理來自異類源的文件,請考慮使用U
標志激活“通用換行符”以打開 。
這是一個例子。 請注意,唯一改變的是要open
的U
參數:
files = ('test_dos.txt', 'test_nix.txt', 'test_mac.txt')
for fname in files:
print("Reading {}".format(fname))
with open(fname, 'U') as fptr:
for line in fptr:
print("--> {!r}".format(line))
print(80*"-")
輸出:
Reading test_dos.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------
Reading test_nix.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------
Reading test_mac.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------
如您所見,並非所有文件的行為都相同。 這可能提示您辣椒U
在那里你正在閱讀的文本文件隨處可見。 但是,我確信有一個很好的理由說明它不是默認的! :)
readline()
例程肯定沒有錯誤; 太多人經常使用它,除非你有一個非常奇怪的實現,而不是標准的Python,你也會使用一個不錯的版本。
您提供的信息還不足以確定您的問題的原因是什么,但有一些分析方法我會建議您找出您正在處理的內容。
你應該仔細看看你的行中有什么,哪些字節終止你的行( '\\n'
或'\\r\\n'
或者其他什么)並且特別仔細看看at+gpsinit=2
處的行和它的行結束。
在Unix系統上,您可以使用od
(或xxd
)。 使用選項-c
打印字符。 使用-t x1 -tc
也可以獲得每個字節的十六進制輸出。
好的,所以我解決了我的問題,似乎Np給了我錯誤的文本文件。 無論如何我用這個命令:
file = open("testtxt.txt", 'r', newline="\r\n")
它給了我好的台詞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.