Python Forum

Full Version: Writing List to CSV
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
What does the file look like in a text editor? Could it be in be just a matter of line endings?
(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)
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.
(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
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!!
(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?