we have the following code: I get the response (ValueError: Mixing iteration and read methods would lose data) Any help
def extract_data(filename):

def extract_data(filename): 
    infile=open(filename, "r")
    #infile.readlines()
    v0 = infile.next() #or []/{}/
    t=[]
    for line in infile.readlines():
        t.append(line)

    return v0, t

v0, t = extract_data("ball_file.txt")

Recommended Answers

Also, next takes an optional default argument so you can easily call v0 = next(infile, None) and check the value of v0 before proceeding...

Jump to Post

All 2 Replies

Try

def extract_data(filename):
    with open(filename) as infile:
        v0 = next(infile)
        return v0, list(infile)

Note that this will fail if there is no line in the file.

Also, next takes an optional default argument so you can easily call v0 = next(infile, None) and check the value of v0 before proceeding...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.