15

I am new to Python, and I want to write a csv file, that lists the roots of my equation. I am working on Sage. My code is :

with open('out.csv', 'w') as f:
    c = csv.writer(f)
    c.writerows(root)

The error I am getting is " NameError: name 'csv' is not defined "

Can anybody help please?

1
  • 3
    you need to do import csv first Commented Jul 6, 2017 at 15:21

2 Answers 2

35

csv is not a builtin, although it's part of the standard library. You need to import it:

import csv

# your code
Sign up to request clarification or add additional context in comments.

Comments

0

I was having similar error using this code:

from csv import QUOTE_MINIMAL, DictWriter

Then I edited the code to:

import csv

now it works perfectly fine for me. I am using python3.

Hope this is helpful

1 Comment

Hey @ann jenny, kudos for making this question more helpful by including other reasons people might get the NameError. I'd suggest updating your answer to show why it's unique. In your case, you were only importing part of the .csv library, and attempting to use parts you hadn't imported was causing the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.