Roy740 Roy740 - 7 days ago 7
Python Question

Python Error: int object is not subscriptable

I was currently messing around with two CSV files. Basically, my goal is to get the values from the third column of the CSV file, only if the first column matches the name, and if the second column matches the date (Will take all from a start date to an end date). I've been testing ways to do it, but I don't quite get my answer since I keep getting the same error.

Here is my current attempt:

name = "Art"
for line in all_words_csv:
if(line[0] == name):
for line in range(1900, 1908):
print(line[2])


Currently in Python 3.5

Thanks.

Edit: I'm expecting this piece to show me all the values in column 3, but the numbers are too big to be true:

for line in all_words_csv:
if(line[0] == name):
for line[1] in range(1900, 1908):
print(line[2])

Answer

You overwrote the line value with the year range.

Without seeing your file, I think you were going for something like this

name = "Art"
for line in all_words_csv:
    if(line[0] == name):
        for year in range(1900, 1908):
            if str(year) == line[2]:
                print(line[2])

Or possibly

name = "Art"
for line in all_words_csv:
    if(line[0] == name):
        if line[2] in map(str, range(1900, 1908))
            print(line[2])