0

I have the following code that works fine:

with open('userWithAgentProp.csv','w+') as f:
        w = csv.DictWriter(f,user_keys)
        w.writeheader()
        for user in userAgentProp_list:
            w.writerow(user)

I used this code to write a function:

def createCSVOutput(fileName, keys, listOfLists):
    with open(fileName, 'w+') as f:
        w= csv.dictWriter(f, keys)
        w.writeheader()
        for row in listOfLists:
            w.writerow(row)

When I call the function:

createCSVOutput('new_test_csv.csv', user_keys, userAgentProp_list)

I get the following error:

File "mongodb_script_2.py", line 101, in <module>
  createCSVOutput('new_test_csv.csv', user_keys, userAgentProp_list)
File "mongodb_script_2.py", line 54, in createCSVOutput
  w= csv.dictWriter(f, keys)
AttributeError: 'module' object has no attribute 'dictWriter'

Why does it work for user_keys variable in the script, but not in the function?

2
  • You probably have something shadowing the csv name. Consider posting more code (or just look for what could be causing it). Commented Apr 29, 2015 at 21:42
  • 2
    I assume you do realize that you didn't capitalize the D? Commented Apr 29, 2015 at 21:43

1 Answer 1

2

You have a typo:

csv.dictWriter is wrong. It should be csv.DictWriter.

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

1 Comment

everybody can do such as mistake.

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.