Python Forum
Reading a csv file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Reading a csv file (/thread-24637.html)



Reading a csv file - julio2000 - Feb-24-2020

I know this is a stupid question but I just can't solve the error I get.

So I've got a file called: Profiles.txt, and I want to open it with python and read what's inside.
This is inside the file:
Main,Henk,Klaas

with open('Profiles.txt', 'r') as file:
    reader = csv.reader(file, delimiter=',')

    for row in reader:
        profile_name = row[0]
        first_name = row[1]
        last_name = row[2]

print(profile_name)
print(first_name)
print(last_name)
So I want my code to return this:
profile_name = 'Main'
First_name = 'Henk'
Last_name = 'Klaas'
It doesn't need to print this out^^, but I want the variables to have those values. But i get this:

Error:
Traceback (most recent call last): File "FILEPATH", line 15, in <module> profile_name = row[0] IndexError: list index out of range
Does someone know how to fix this and how to give the variables the value I want them to have?


RE: Reading a csv file - buran - Feb-24-2020

look inside your Profiles.txt file
probably there are empty lines ate the end of the file or the format is not what you expect