Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing List to CSV
#1
Hi,

I have a list as follows stored in a variable name csvImport:

[['3-March-17', 'a', 'b', 'c'],
['16-February-17', 'd', 'e', 'f'],
['19-January-17', 'g', 'h', 'i']]

I use the following code to write to a csv file.

with open(r"C:\folder\myFile.csv", "w") as csvFile:
writer = csv.writer(csvFile, delimiter=",")
for item in csvImport:
writer.writerow(item)
However, when I use the code to write it to a csv file, and I opened the csv file with Excel, it seems to add  an additional row in between the original rows of data.

So when I read the created csv file back to Python, the list would look like this:

[['3-March-17', 'a', 'b', 'c'],
[]
['16-February-17', 'd', 'e', 'f'],
[]
['19-January-17', 'g', 'h', 'i']]

What can I do to prevent new rows being added when I write a list to a csv file?

Thank you!
Reply
#2
What does the file look like in a text editor? Could it be in be just a matter of line endings?
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
#3
(Apr-05-2017, 10:20 PM)Ofnuts Wrote: What does the file look like in a text editor? Could it be in be just a matter of line endings?

When I opened it on a notepad it looks like this:

3-March-17, a, b, c
16-February-17, d, e, f
19-January-17, g, h, i

I tried to filter out the newline when reading the data back to Python using the following code but I still get unwanted additional row in between my original data.

[['3-March-17', 'a', 'b', 'c']
[] ##this is not in the original data
['16-February-17', 'd', 'e', 'f']
[] ##neither is this
['19-January-17', 'g', 'h', 'i']]
with open(r"[color=#0000ff][size=small][font=Consolas,]C:\folder\myFile.csv[/font][/size][/color]") as csvFile:
  csvReader = csv.reader(csvFile)
     for row in csvReader:
        if row != "\n" :
          csvImport2.append(row)
Reply
#4
Try:
with open(r"C:\folder\myFile.csv", "w") as csvFile:
writer = csv.writer(csvFile, delimiter=",")
writer.writerows(csvImport)
csvImport have to be iterable and the objects in this iterable have to be formatted properly.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Apr-06-2017, 02:35 AM)tkj80 Wrote: When I opened it on a notepad it looks like this:

3-March-17, a, b, c
16-February-17, d, e, f
19-January-17, g, h, i
Then you should not have new line.
If i just copy data from you post to foo.csv.
import csv

with open('foo.csv') as csvfile:
    reader = csv.reader(csvfile, skipinitialspace=True, delimiter=',')
    for row in reader:
        print(row)
Output:
['3-March-17', 'a', 'b', 'c'] ['16-February-17', 'd', 'e', 'f'] ['19-January-17', 'g', 'h', 'i']
So you should try save data again,
if there had been a new line and want to remove it.
import csv

with open('foo.csv') as csvfile:
    reader = csv.reader(csvfile, skipinitialspace=True, delimiter=',')
    for row in reader:
        if row == []:
            pass
        else:
            print(row)
Read and write:
import csv

with open('foo.csv') as csvfile,open('bar.csv', 'w') as f_out:
    reader = csv.reader(csvfile, skipinitialspace=True, delimiter=',')
    writer = csv.writer(f_out, lineterminator='\n', )
    for row in reader:
        writer.writerow(row)
Output:
3-March-17,a,b,c 16-February-17,d,e,f 19-January-17,g,h,i
Reply
#6
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!!
Reply
#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


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,630 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  Help|CSV Writing on List with Inner Commas soothsayerpg 2 2,393 Jul-20-2019, 06:59 AM
Last Post: scidam
  Help writing code to create a ranked list swilson421 2 2,474 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,405 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