Python Forum
[split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'?
#1
i am catherine. I just now started to learn python.
import random
import csv
a={"1":1}
print(len(a))

for i in range(100):
    x=random.randint(1, 100)
    a[i] = x
    print(a)
   
with open('random.csv', 'w', newline='') as csvfile:
    fields = ['QNO', 'RQNO']
    writer = csv.DictWriter(csvfile, fieldnames=fields)
    writer.writeheader()
for i in range(101):
    #j=a[i]
    #print(i,j)
    writer.writerow({'QNO':i , 'RQNO': a[i]})
file.close()    
i get err
Error:
File "e:\phy-prg\4.py", line 18, in <module> csvwriter.writerow({'QNO':i , 'RQNO': a[i]}) NameError: name 'csvwriter' is not defined. Did you mean: 'writer'?
Reply
#2
(Sep-01-2022, 12:51 PM)cathy12 Wrote: i am catherine. I just now started to learn python.
Hi @cathy12 and welcome to the forum. Please start a new thread describing the issue: go to the General Coding Help forum, or perhaps the homework forum if this is homework and push the « Post Thread » button. Don't forget to use Code Tags to display Python code in your post.
Reply
#3
(Sep-01-2022, 12:51 PM)cathy12 Wrote: i get err
The error message is not consistent with the code. Also the for loop should be indented inside the with open... section because it uses the object writer which exists only in this with section.
Reply
#4
On the face of this (as I've not in fact tested it) I can see that the file is being close at line 14: you're using a context manager with open() as and as such you don't need line 19, which is the way to do things, so you'll want to move the for loop, which starts at line 15, inside the file context manager: simple indent the loop by four spaces.

See how that goes and report back if you have any more issues.

n.b: I've not tested any of this, so there could be another bug or two to squish.

edit to add...

I've just spotted this at line 6: for i in range(100): Change that to: for i in range(101):
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
You are not using DictWriter the way it is meant to be used. csv.DictWriter can be used that way, but it is far easier to use a csv.writer object to do what you want.
import random
import csv

with open("random.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(("QNO", "RQNO"))  # Writing the column headers
    for index in range(5):
        writer.writerow((index, random.randint(1, 100)))
random.csv
Output:
QNO,RQNO 0,3 1,56 2,69 3,6 4,56
DictWriter is used when you already have dictionaries that you want to write.
import random
import csv

# Make some fake dictionary data.  Add an extra field for DictWriter to filter out.
random_dict = [{"QNO": i, "RQNO": random.randint(1, 100), "ZERO":0} for i in range(5)]  

with open("random.csv", "w", newline="") as csvfile:
    fields = ("QNO", "RQNO")
    writer = csv.DictWriter(csvfile, fieldnames=fields, extrasaction="ignore")
    writer.writeheader()
    writer.writerows(random_dict)
random.csv
Output:
QNO,RQNO 0,91 1,27 2,78 3,6 4,87
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 311 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,122 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,913 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  Csv writer meaning of quoting mg24 2 1,163 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,522 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
Sad pandas writer create "corrupted" file freko75 1 2,831 Jun-14-2022, 09:57 PM
Last Post: snippsat
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,935 May-13-2022, 03:37 PM
Last Post: deanhystad
  CSV writer - no output?? Clives 6 2,578 Mar-30-2022, 04:09 PM
Last Post: Clives
  NameError: name 'cross_validation' is not defined tmhsa 6 13,374 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 15,030 Oct-20-2021, 05:39 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020