Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv reader
#1
I'm very early on in python, but I'm trying to process a csv file with this code:
# importing csv module 
import csv 
  
# csv file name 
filename = "admitOrders.csv"
  
# initializing the titles and rows list 
fields = [] 
rows = [] 
  
# reading csv file 
print('\nKGG - First 5 rows are:\n')
with open(filename, 'r') as csvfile: 
    # creating a csv reader object 
    csvreader = csv.reader(csvfile) 
      
    # extracting field names through first row 
    fields = csvreader.next()
I'm getting this error:

Error:
Traceback (most recent call last): File "processCSV.py", line 18, in <module> fields = csvreader.next() AttributeError: '_csv.reader' object has no attribute 'next'
Any suggestions?

Thanks

K
Reply
#2
fields = next(csvreader)
this would do, however I would prefer to use csv.DictReader instead of csv.reader
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
please visit this link. ALL ABOUT CSV IN PYTHON this all what you need from basic to advance:
Reply
#4
While choosing between different options while reading data from text files I have found useful following those loose rules:

- if data in file is 'table with header' -> use csv.DictReader (maps automagically)
- if data in file is 'table without header' -> provide headers and use csv.DictReader
- if data in file is not structured and has quotes '"' and/or other ('weird') formatting -> use csv.reader (handles these automagically)
- if data on rows is not structured -> iterate over fileobject rows directly and do your own magic

... and then there is pandas :-).

No 'one obvious way' here. Use tools you comfortable with.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xml simple reader kucingkembar 2 1,050 Aug-19-2022, 08:51 PM
Last Post: kucingkembar
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,472 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,465 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  NFC reader code help johnroberts2k 1 2,565 Jul-02-2021, 08:43 PM
Last Post: deanhystad
  csv.reader(): Limit the number of columns read in Windows Pedroski55 9 5,166 Jan-23-2021, 01:03 AM
Last Post: pjfarley3
  Closing Files - CSV Reader/Writer lummers 2 2,598 May-28-2020, 06:36 AM
Last Post: Knight18
  G code reader luisfelipepc 2 3,674 Aug-13-2018, 02:56 AM
Last Post: ichabod801
  Python code for gcode reader and representation ralmeida 1 6,235 Jul-31-2018, 09:20 AM
Last Post: DeaD_EyE
  AttributeError: module 'csv' has no attribute 'reader' python1234 2 26,908 Jun-08-2018, 06:13 AM
Last Post: python1234
  PDf reader jorgelameira 2 3,923 Mar-24-2018, 02:02 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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