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")
    v0 = infile.next() #or []/{}/
    for line in infile.readlines():

    return v0, t

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

Recommended Answers

All 2 Replies


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 developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.