Jan Solo
Jan Solo

Reputation: 13

Reading a text file with multiple columns?

I am suppose to read a large text file that consists of states, years, quarters and index. However when I run my code it receives an error saying "not enough values to unpack (expected 4, got 2). Any ideas on where I may be going wrong?

def read(filepath):
    data = {}
    fd = open(filepath)
    for line in fd:
        state, year, qtr, index = line.split()
        if len(state) == 2:
            if index != '.':
                if state not in data:
                    data[state] = [QuarterHPI(int(year), int(qtr), float(index))]
    print(data)
    return data

Upvotes: 0

Views: 93

Answers (1)

Leandr  Khaliullov
Leandr Khaliullov

Reputation: 46

state, year, qtr, index = line.split()

is waiting for 4 items, but line.split() seems returned only 2.

Upvotes: 1

Related Questions