Warning: session_start(): open(/tmp/sess_e5238c5f76cb529f1795f2cc5764201b, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Resolve AttributeError module 'csv' has no attribute 'reader' in Python - LearnShareIT

How To Resolve AttributeError module ‘csv’ has no attribute ‘reader’ in Python

What causes and ways to resolve the error AttributeError module ‘csv’ has no attribute ‘reader’ in Python. If you are looking for a solution to this problem then this article is for you.

What causes the AttributeError module ‘csv’ has no attribute ‘reader’ error?

The syntax to use the csv.reader function to read CSV files in Python:

csv.reader(f)

The csv.reader() function returns an iterator that contains lists with each list being the contents of a single line read from the CSV file.

The error AttributeError module ‘csv’ has no attribute ‘reader’ occurs because you create a local file with the same name as the module you are importing, resulting in the operation of that module being affected.

Example

Name, Age, Vacations
John, 18, Student
Peter, 18, Student
Frank, 18,Student
import csv
import os.path

scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'infor.csv')

with open(filename, 'rt') as f:
   # Read csv file using csv.reader function
    exportData = csv.reader(f)
    for i in exportData:
        print(i)

Output:

Traceback (most recent call last):
  File "d:\vscode\Python\csv.py", line 1, in <module>
    import csv
  File "d:\vscode\Python\csv.py", line 9, in <module>
    exportData = csv.reader(f)
                 ^^^^^^^^^^
AttributeError: partially initialized module 'csv' has no attribute 'reader' (most likely due to a circular import)

The main cause is that the local file ‘csv.py’ hides the functionality of the csv module.

How to solve this error?

Delete or change the names of local files.

To handle the error, change the local filename ‘csv.py’ to something else that does not match the module name.

Example:

  • I will change the filename ‘csv.py’ to ‘readfilecsv.py’.
import csv
import os.path

scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'infor.csv')

with open(filename, 'rt') as f:
   # Read csv file using csv.reader function
    exportData = csv.reader(f)
    for i in exportData:
        print(i)

Output:

['Name', ' Age', ' Vacations']
['John', ' 18', ' Student']
['Peter', ' 18', ' Student']
['Frank', ' 18', 'Student']

Use pandas module.

You can use the pandas module to read a csv file in Python. To read the csv file, use the pandas.read_csv function (csv file name). The pandas.read_csv function will return a table of data read from the csv file.

Example:

import pandas
import os.path

scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'infor.csv')

with open(filename, 'rt') as f:
   # Read the csv file using pandas.
    exportData = pandas.read_csv(filename)
    print(exportData)

Output:

    Name   Age  Vacations
0   John    18    Student
1  Peter    18    Student
2  Frank    18    Student

Note: You also must not name the local file with the name of the pandas module while using it. This also results in the error AttributeError module ‘pandas’ has no attribute ‘read_csv’.

Summary

My post is over, and the AttributeError module ‘csv’ has no attribute ‘reader’ has been solved. The point here is that you must avoid blocking or dynamic names of the module. Hope the article is helpful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *