在 python 中读入文件

read in file in python

我在 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

您读取的最后一行不会 return \n 而是一个空字符串,表示文件已被完全读取。

为什么不使用类似

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

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

然后您可以分别分析每一行。

例如,您可以一次性逐行读取所有文件以获得您需要的信息。

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

for x in lines:
    print x

file.read() 一次读取整个文件,一旦到达文件末尾 file.read() 将 return 一个空字符串。所以它 never 将等于 \n\r\n 因此永远不会跳出 while 循环。

如果您想遍历行直到段落结束,您可以使用:

paragraph = ""

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

print(paragraph)

您的 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