简体   繁体   中英

Writing multiple blank fields to csv python

I have a large number of CSVs I am trying to import into a DB table.

My import template asks for a set group of columns. If my input data only has a few of the desired columns(in my case only 3), I want to put them in their respective columns and leave the unsatisfied ones blank.

For example, the list List1Column will go into "List1Column" of my template and so on. Since this input data does not contain data for my other columns, 'OtherColumn','OtherColumn2' etc., I just want to make them empty. Since I have compiled my input data into lists (each effectively holding a column of data), I am zipping them into the order I want them in my template.

For the empty columns, I am having to feed my template an empty list for each one, empty,empty,empty,empty,empty,empty , in my zip iteration. Is there a better way to do this? Can I just say 'empty 5 times' instead of empty,empty,empty,empty,empty,empty .

My output is the same either way, I just know my method of going about this is poor practice and would like to clean up my code. I have provided sample csv input with code and output.

Input Data

$ cat testcsv.csv  

numbers,AthruZ,LthruN  
1,a,l  
2,b,m  
3,z,n  

Code

import csv
from itertools import izip

huckFin = open('testcsv.csv','rb')
huckCin = csv.reader(huckFin, delimiter=',', quoting=csv.QUOTE_NONE  )
csvdata = [row for row in huckCin]

List1Column = [row[0] for row in csvdata]
List2Column = [row[1] for row in csvdata]
List3Column = [row[2] for row in csvdata]

empty = ['' for row in csvdata]

with open('file.csv', 'wb') as fout:
    csvout = csv.writer(fout, delimiter = ',',
    lineterminator = '\n',
    quotechar = '"'
    )

# My template
csvout.writerow(["List1Column",
                 "OtherColumn",
                 "OtherColumn2",
                 "OtherColumn3",
                 "OtherColumn4",
                 "OtherColumn5",
                 "OtherColumn6",
                 "List2Column",
                 "List3Column"])

csvout.writerows(izip(List1Column,
                      empty,
                      empty,
                      empty,  # Is there a way
                      empty,  # to avoid this list
                      empty,  # of empty columns?
                      empty,
                      List2Column,
                      List3Column))

Output

List1Column,OtherColumn,OtherColumn2,OtherColumn3,OtherColumn4,OtherColumn5,OtherColumn6,List2Column,List3Column  
numbers,,,,,,,AthruZ,LthruN  
1,,,,,,,a,l  
2,,,,,,,b,m  
3,,,,,,,z,n  

Also, I would like to skip the header row. In perl I would use:

next if $.==1  

before looping through the file given the header is the first line. I assume there is an equivalent in Python. I am also getting an additional new line in my output ... naturally in perl I would go:

chomp($output) if eof

I would also assume there is a python equivalent to that as well. $output being my csvout object.

If anyone has a better suggestion on how to do this differently or more efficiently overall, let me know.

Try print str(empty) * 5 .

Multiplication as you expect it only works for strings.

You can do this using a simple while :

empty = []
i = 0
while i < 5:
    print empty
    i = i + 1

Use a for loop.

for _ in range(5):
    print listname,

Note that using the comma in the print command means they'll all be on the same line (which it seems like you want).

>>> from __future__ import print_function
>>> print(*[empty] * 5)
[] [] [] [] []

You could stringify empty and then use this print a string x-times option eg

empty = []
print 5*str(empty)  

You might want to look into itertools. For example:

import itertools
a=[]
repeat=list(itertools.repeat(a, 10))
print(repeat)

Should give you:

[[], [], [], [], [], [], [], [], [], []]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM