I am currently trying to copy an entire column from a single .csv file to another new .csv file. That sounds rather simple but I keep getting a problem.
import csv
import glob
with open(r"C:\Users\n47-jones\Dropbox\Nevil Programming\test1.csv", "wb") as outfile:
writer = csv.writer(outfile, delimiter=",")
for csvfilename in glob.glob(r"C:\Users\n47-jones\Dropbox\Nevil Programming\testdir\*.csv"):
with open(csvfilename, "rb") as infile:
reader = csv.reader(infile)
for row in reader:
writer.writerow(row[1])
when this is output in the new test1.csv the contents of row[1] is split down into each individual letter.
eg
it goes from:
DATE |
to
D | A | T | E
I hope this makes some sense. Thanks for any help.
Nick
write.writerow() takes a iterable and writes it as a csv row. A string is a iterable that yield its characters.
So your problem is that instead of giving it a list you are giving it a string. If you want the csv to contain only the column you have to give it a iterable with 1 item:
writer.writerow([row[1]])
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.