Python Forum
CSV reader : save the content into a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV reader : save the content into a list
#1
I want to read a csv file and save the content in a list call 'data'. The following reads the data but do not save a list with the content.

import csv
with open('file.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter =';')
I believe this is very basic yet I couldn't find the answer after a google search without too advanced coding.
Reply
#2
import csv

with open('file.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')
    for row in readCSV:
        print(row)
Are you sure the delimiter is a ';' ?
Reply
#3
(Oct-18-2017, 10:42 PM)Larz60+ Wrote:
import csv

with open('file.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')
    for row in readCSV:
        print(row)
Are you sure the delimiter is a ';' ?

Hmm. My csv file is a multidimensional table (252 rows and 51 columns). When I run that code it only saves the last row. How do I fix that?
Reply
#4
I tried this with a csv file and all rows were listed.
Perhaps your file is not truly a csv file?
Reply
#5
(Oct-23-2017, 04:55 PM)econmajor Wrote: Hmm. My csv file is a multidimensional table (252 rows and 51 columns). When I run that code it only saves the last row. How do I fix that?

The variable row contains only the current data of iteration step. After leaving the for loop, row contains the last row. If you want to add the data to a list, then add the data to a list.

before you iterating over the rows, you create an empty list and assign it to a name data for example.
data = []
In each iteration step you append the current rot to the list object.

data.append(row)
If you just want to read the whole csv into memory without any processing between, you can have this cheaper.

import csv

with open('file.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=';')
    data = list(readCSV)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy xml content from webpage and save to locally without special characters Nik1811 14 929 Mar-26-2024, 09:28 AM
Last Post: Nik1811
  [solved] list content check paul18fr 6 706 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  how to save to multiple locations during save cubangt 1 555 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  xml simple reader kucingkembar 2 1,062 Aug-19-2022, 08:51 PM
Last Post: kucingkembar
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,486 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,483 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  NFC reader code help johnroberts2k 1 2,587 Jul-02-2021, 08:43 PM
Last Post: deanhystad
  csv.reader(): Limit the number of columns read in Windows Pedroski55 9 5,225 Jan-23-2021, 01:03 AM
Last Post: pjfarley3
Question Save list with nested list into CSV SpongeB0B 1 5,385 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  Closing Files - CSV Reader/Writer lummers 2 2,610 May-28-2020, 06:36 AM
Last Post: Knight18

Forum Jump:

User Panel Messages

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