我有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
這將工作,但我想讀入一個字符串的數據塊。不只是一行一行。所以我想讓它讀到第一個空行然後從循環中斷開。這可能嗎? – user4959809
在我回復的代碼之後,你必須做的唯一事情就是循環遍歷行,並檢測一個空行。添加到回覆 – jcoppens