Python3: Module csv has no Dictwriter Member

Asked

Viewed 62 times

1

Hello. I am following the reading of the book Data Visualization with Python and Javascript from Scratch by Kyran Dale (O'Reilly). In the following code, it is understood that a . csv file should be saved, using the Python csv library.

Using Anaconda2

import csv
import os.path

nobel_winners = [
    {'category': 'Physics',
     'name': 'Albert Einstein',
     'nationality': 'Swiss',
     'sex': 'Male',
     'year': 1921},
    {'category': 'Physics',
     'name': 'Paul Dirac',
     'nationality': 'British',
     'sex': 'Male',
     'year': 1933},
    {'category': 'Chemistry',
     'name': 'Marie Curie',
     'nationality': 'Polish',
     'sex': 'Female',
     'year': 1911}
]

mypath = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(mypath, './data/nobel_winners.csv')

with open(path,'wb') as f:
    fieldnames = nobel_winners[0].keys()
    fieldnames.sort()
    writer = csv.DictWriter(f,fieldnames=fieldnames)
    writer.writeHeader()
    for w in nobel_winners:
        writer.writerow(w)

However, what appears to me in pylint is the exit:

Module 'csv' has no 'Dictwriter' Member

How can I fix this?

The book uses Python 2.7 as default, but even in the documentation I found nothing that marked this member as deprecated or something like that.

1 answer

1


I made a small change to your code and it worked here, see below the versions I’m using:

$ python --version
Python 3.6.7 :: Anaconda, Inc.
~ 
$ python -c "import csv; print(csv.__version__)"
1.0

Now the adapted code, I comment on the changes in the code itself:

# Ao invés de abrir o aruivo com "wb", abro somente com "w"
with open(path,'w') as f:
    fieldnames = nobel_winners[0].keys()
    writer = csv.DictWriter(f,fieldnames=fieldnames)
    writer.writeheader()  # ==> Aqui vc usa writeHeader ao invés de writeheader
    for w in nobel_winners:
         writer.writerow(w)

Testing the output of the generated file:

$ cat nobel_winners.csv 
category,name,nationality,sex,year
Physics,Albert Einstein,Swiss,Male,1921
Physics,Paul Dirac,British,Male,1933
Chemistry,Marie Curie,Polish,Female,1911

Obs.:
Delete the line fieldnames.sort() because the keys are already "drawn" and, on the other hand, there is no Sort method in dictkeys but if you want to use this line, use Sorted:

fieldnames = sorted(fieldnames)

See the syntax of DictWriter.writeheader() here

  • Your code is correct, however, it still shows the same message. In addition to this, I realized that I was actually running Anaconda2 (python 2.7). To try one last time, I’m downloading Anaconda3. Thank you for your reply, which incidentally was quite accurate (I’m still getting used to the syntax and language of python).

  • 1

    You must be making some other mistake (or it’s different versions), here running without errors. Detail: With anaconda you can have several versions of python. You can even install anaconda 3 and run python 2.7 and all versions of the packages you have in the book you are studying. See that answer

  • Actually the newest version of anaconda is 4.x.

  • 1

    In fact, I was with the Python version problem, now I have updated Anaconda, and it worked. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.