2015-06-07 39 views
1

我有Python中的功能,在這看起來是這樣的文件解析:讀取文件中的巨蟒

Led Zeppelin 
1979 In Through the Outdoor 
-In the Evening 
-South Bound Saurez 
-Fool in the Rain 
-Hot Dog 
-Carouselambra 
-All My Love 
-I'm Gonna Crawl 

Led Zeppelin 
1969 II 
-Whole Lotta Love 
-What Is and What Should Never Be 
-The Lemon Song 
-Thank You 
-Heartbreaker 
-Living Loving Maid (She's Just a Woman) 
-Ramble On 
-Moby Dick 
-Bring It on Home 

Bob Dylan 
1966 Blonde on Blonde 
-Rainy Day Women #12 & 35 
-Pledging My Time 
-Visions of Johanna 
-One of Us Must Know (Sooner or Later) 
-I Want You 
-Stuck Inside of Mobile with the Memphis Blues Again 
-Leopard-Skin Pill-Box Hat 
-Just Like a Woman 
-Most Likely You Go Your Way (And I'll Go Mine) 
-Temporary Like Achilles 
-Absolutely Sweet Marie 
-4th Time Around 
-Obviously 5 Believers 
-Sad Eyed Lady of the Lowlands 

它應該在文件中讀取,直到達到新的生產線,然後停止閱讀它並打印它讀過的內容。但是,由於某種原因,它陷入了閱讀新行的無限循環中,我無法確定原因。會有一個簡單的解決辦法嗎?也許是我忽略的小東西?任何幫助將不勝感激!

def parseData() : 
    filename="testdata.txt" 
    file=open(filename,"r+") 

    while file.read() not in ['\n', '\r\n']: 
     album=file.read() 
    print album 

回答

1

您讀取的最後一行不會返回\ n,而是一個空字符串,表示文件已被完整讀取。

爲什麼不使用類似

with open("testdata.txt") as infile: 
    lines = infile.readlines() 

block = "" 
for line in lines: 
    if line.strip() == "": break 
    block += line 

那麼你可以單獨分析每一行。

+0

這將工作,但我想讀入一個字符串的數據塊。不只是一行一行。所以我想讓它讀到第一個空行然後從循環中斷開。這可能嗎? – user4959809

+0

在我回復的代碼之後,你必須做的唯一事情就是循環遍歷行,並檢測一個空行。添加到回覆 – jcoppens

1

例如,您可以一次一行讀取所有文件,以獲取您需要的信息。

lines = [line.rstrip('\n') for line in open(filename)] 

for x in lines: 
    print x 
+0

不是一個好方法,它會在大文件中產生問題 –

+0

嗯,是的。你是對的。 – Yurrili

1

file.read()一次讀取整個文件,一旦你已經達到了文件file.read()結束後將返回一個空字符串。所以它的永遠不會將等於\n\r\n,因此永遠不會擺脫while循環。

如果通過線,直到一個段落的結尾要循環您可以使用:

paragraph = "" 

for line in f: 
    if line in ["\n", "\r\n"]: 
     break 
    paragraph += line 

print(paragraph) 
0

你file.read()不是[ '\ n', '\ r \ n' ],因爲它包含整個文件。你可以使用:

filename="text.txt" 
block = [] 
for line in open(filename): 
    block.append(line) 
    if line in ('\n', '\r\n'): 
     print(block) 
     block=[] # remove if you want to use the content of the block 
     break #stops the loop - remove if you want all blocks printed