I have csv file. When I open it by VisualStudio, i have something like this:
nr1 nr2 nr3 name place
10 01 02 Jack New York
10 01 03 David London
10 02 01 Jasmine New Jersey
There are two spaces between elements in every line and one space in name of the place.
When I open csv file by Excel, there are just elements in every other column.
I don't know how can I read this csv file to make every line a list of elements like this:
my_list = [
['10','01','02','Jack','New York']
['10','01','03','David','London']
['10','02','01','Jasmine','New Jersey']]
Cause I want to make some loop after this:
for line in my_list:
nr1 = line[0]
nr2 = line[1]
nr3 = line[2]
name = line[3]
place = line[4]
And here I will make an instance of the class for every line.
How can I do that?
Using the
csv
module makes doing it fairly easy. The only trick was to make the two-letter delimiter used in the input file appear to be a single character,','
, in the example, because it the default.Output:
They are tab delimited csv file. Meaning that each column is separated by tab ('\t').
The following code should work.
Updated: use .rstrip() to remove
\n
Try this
Try this, using the csv module and its
reader()
method: