The function to read the CSV file in python causes an error.

Asked 2 years ago, Updated 2 years ago, 49 views

I'm trying to create a function to read CSV files in python, but somehow I get a strange error.Is there any way to improve this?

defcsv_input(csv):
    data = [ ]
    f = open(csv, 'r')
    reader=csv.reader(f)
    For row in reader:
        data.append(row)
    return data

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "makes.py", line 70, in input_datas
  data=csv.reader(f)
AttributeError: 'str' object has no attribute' reader'

I get this error.I don't know why it comes out.

python csv

2022-09-30 19:48

1 Answers

The names of the csv and the csv module in the argument conflict.

reader=csv.reader(f) may mean calling reader() in the csv module, but calling reader() with csv argument, so it gets 'str'object has no attribute'.


2022-09-30 19:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.