user4959809
user4959809

Reputation: 65

read in file in python

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

Upvotes: 1

Views: 1046

Answers (4)

mmensing
mmensing

Reputation: 311

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

Upvotes: 0

jcoppens
jcoppens

Reputation: 5440

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.

Upvotes: 1

Mathieu David
Mathieu David

Reputation: 5268

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)

Upvotes: 1

Yurrili
Yurrili

Reputation: 338

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

Upvotes: 1

Related Questions