1

I have a function in python that parses in a file that looks like this:

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

It is supposed to read in the file until it hits the new line and then stop reading it and print what it has read. But, for some reason it is stuck in an infinite loop of reading new lines and I cannot pinpoint why. Would there be a simple fix for this? Maybe something small I am overlooking? Any help would be greatly appreciated!

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

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

4 Answers 4

1

The last line of your read(s) will not return \n but an empty string, to indicate that the file was read completely.

Why not use something like

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

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

you can then analyze each line separately.

Sign up to request clarification or add additional context in comments.

2 Comments

This would work but I want to read in the block of data into one string. Not just line by line. So I want it to read down to the first empty line and then break from the loop. Would that be possible?
The only thing you have to do, after the code from my reply, is loop over the lines, and detect an empty line. Added to the reply
1

For example you can read all file line by line at once to get information which you need.

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

for x in lines:
    print x

Comments

1

file.read() reads the entire file at once and once you have reached the end of the file file.read() will return an empty string. So it's never going to be equal to \n or \r\n and thus never break out of the while loop.

If you want to loop through the lines until the end of a paragraph you can use:

paragraph = ""

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

print(paragraph)

Comments

0

Your file.read() is not in ['\n', '\r\n'] as it contains the whole file. You could use:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.