2014-03-04 20 views
1

我有一個readline()方法的問題,它有時會返回2行而不是一個,我不知道爲什麼。有人能幫我嗎 ?錯誤的readline()在Python

這裏文本文件我看過(用記事本)的一部分:

at+gpsinit=2 
OK 

+GPSEVINIT: 1 
at+gpsnmea=3 
OK 
at+gpsstart=0 
OK 

並用記事本++:

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外殼有:

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() 
+2

恩,對我來說很好。 –

+0

請注意,「at + gpsinit = 2」和「OK」之間沒有行跳,所以我不明白爲什麼會有17個「空白」行。 – katze

+1

emty線也是線。 –

回答

0

好吧,所以我解決了我的問題,似乎Np給我錯誤的文本文件。無論如何,我用這個命令:

file = open("testtxt.txt", 'r', newline="\r\n") 

它給了我很好的線條。

1

readline()例程中肯定沒有錯誤;太多的人經常使用它,除非你有一個非常奇怪的實現,它不是標準的Python,否則你也會使用一個體面的版本。

您提供的信息還不足以成爲當然您的問題的原因是什麼,但有一些分析方法,我會建議找出您正在處理的內容。

你應該仔細看看你的線路中有哪些字節終止你的線路('\n''\r\n'或其他什麼),並特別仔細看看線路at+gpsinit=2及其結尾。

在Unix系統上,您可以使用od(或xxd)。使用選項-c打印字符。使用-t x1 -t c也可以爲每個字節獲得十六進制輸出。

+0

是的,謝謝,我得出了同樣的結論,我用Notepad ++而不是記事本讀了文件,實際上在第16行末有一個'CR',在第17行有'CR LF 」。 – katze

2

您遇到的問題很可能是由於行尾標記存在問題。

  • 視窗/ DOS通常使用CRLF(或,\r\n,或0d0a以字節爲單位)。
  • Unix的通常使用LF(或\n,或以字節0a
  • MacOS的通常使用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 

廣告你可以看到,單詞hello68 65 6c 6c 6f)後面跟着不同的字節分別爲,0a0d。在MS記事本中編輯文件時,最有可能會插入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參數open

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。不過,我確信有一個很好的理由,它不是默認的! :)