Posts: 25
Threads: 15
Joined: Oct 2016
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!
Posts: 687
Threads: 37
Joined: Sep 2016
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
Posts: 25
Threads: 15
Joined: Oct 2016
Apr-06-2017, 02:35 AM
(This post was last modified: Apr-06-2017, 03:33 AM by tkj80.)
(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)
Posts: 2,953
Threads: 48
Joined: Sep 2016
Apr-06-2017, 05:21 AM
(This post was last modified: Apr-06-2017, 05:21 AM by wavic.)
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.
Posts: 7,319
Threads: 123
Joined: Sep 2016
Apr-06-2017, 10:04 AM
(This post was last modified: Apr-06-2017, 10:04 AM by snippsat.)
(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
Posts: 25
Threads: 15
Joined: Oct 2016
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!!
Posts: 687
Threads: 37
Joined: Sep 2016
(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
|