Python csv.reader (filename) REALLY returns a list? Does not look like

So, I'm still learning Python, and now I'm learning to read csv files. The lesson I just watched tells me that using

csv.reader(filename)

returns a list.

So, I wrote the following code:

import csv
my_file = open(file_name.csv, mode='r')
parsed_data = csv.reader(my_file)
print(parsed_data)

and what he prints,

<_csv.reader object at 0x0000000002838118>

If what it outputs is a list, should I not get a list, i.e. something like that?

[value1, value2, value3]
+4
source share
2 answers

What you get is iterable , that is, an object that will give you a sequence of other objects (in this case strings). You can loop it foror use it list()to get the actual list:

parsed_data = list(csv.reader(my_file))

, , , , , , ( , , , ). , , , .

+11

csv.reader ( ).

, :

import csv
my_file = open(file_name.csv, mode='r')
parsed_data = csv.reader(my_file)
for row in parsed_data:
    print(row)   # <--- a list of strings
+4

Source: https://habr.com/ru/post/1619762/


All Articles