Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing List to CSV
#7
(Apr-10-2017, 08:19 PM)tkj80 Wrote:
import csv

with open('foo.csv') as csvfile:
    reader = csv.reader(csvfile, skipinitialspace=True, delimiter=',')
    for row in reader:
        if row == []:
            pass
        else:
            csvImport2.append(row)
This code helped me to eliminate [] that seems to possibly came from new line when I tried to read the csv data into Python.

When I opened the csv file in notepad, it does come out as below:
Output:
3-March-17, a, b, c 16-February-17, d, e, f 19-January-17, g, h, i
So I'm not sure why without this line of code:  if row == []: pass
when I try to read the data from the csv file, it would look like this:

Output:
[['3-March-17', 'a', 'b', 'c'] []  ['16-February-17', 'd', 'e', 'f'] []  ['19-January-17', 'g', 'h', 'i']]
Thank you for your help!!

Can you zip the CSV (or a small part that sill exhibits the problem) and attach it here?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
Writing List to CSV - by tkj80 - Apr-05-2017, 08:25 PM
RE: Writing List to CSV - by Ofnuts - Apr-05-2017, 10:20 PM
RE: Writing List to CSV - by tkj80 - Apr-06-2017, 02:35 AM
RE: Writing List to CSV - by wavic - Apr-06-2017, 05:21 AM
RE: Writing List to CSV - by snippsat - Apr-06-2017, 10:04 AM
RE: Writing List to CSV - by tkj80 - Apr-10-2017, 08:19 PM
RE: Writing List to CSV - by Ofnuts - Apr-11-2017, 11:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 2,671 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  Help|CSV Writing on List with Inner Commas soothsayerpg 2 2,418 Jul-20-2019, 06:59 AM
Last Post: scidam
  Help writing code to create a ranked list swilson421 2 2,499 Jul-16-2018, 04:51 PM
Last Post: swilson421
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,446 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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