我尝试将多个具有相同格式的csv文件合并为一个。
merge_list = glob.glob(gndlbsum+"*gndlbsum.csv")
filewriter_lbsum = target_dir+"gndlbsum_master.csv"
#get the list of csv files and set the output file
counter=0
for file in merge_list:
with open(file,"rU") as csv_file:
filereader = csv.reader(csv_file)
with open(filewriter_lbsum,"a") as f:
writer = csv.writer(f, delimiter = "|")
#check to see if it's the first file, if it is, add header,
#otherwise skip first row
if counter<1:
for row in filereader:
writer.writerow(row)
counter+=1
else:
header = next(filereader,None)
for row in filereader:
writer.writerow(row)
当我这样做时,输出csv中的每一行都完全用双引号括起来,我尝试使用list.append(row)
代替,但它没有区别,因为该行用双引号括起来。有没有办法避免这种情况?
修改
以下是源文件的示例:
COL1|COL2|COL3
1|2|3
4|5|6
输出:
"COL1|COL2|COL3"
"1|2|3"
"4|5|6"
答案 0 :(得分:5)
我认为这将消除由于没有告诉正在创建的csv.reader
输入文件中的分隔符是"|"
个字符而不是默认值{{1}而引起的引号字符。
","
答案 1 :(得分:1)
csv模块在每行末尾自动添加双引号。如果不确切知道您的csv文件是什么样的,请尝试以下方法:
merge_list = glob.glob(gndlbsum+"*gndlbsum.csv")
filewriter_lbsum = target_dir+"gndlbsum_master.csv"
#get the list of csv files and set the output file
counter=0
for file in merge_list:
with open(file,"rU") as csv_file:
filereader = csv.reader(csv_file, skipinitialspace=True)
with open(filewriter_lbsum,"a") as f:
writer = csv.writer(f,delimiter = "|", quoting=csv.QUOTE_NONE)
#check to see if it's the first file, if it is, add header,
#otherwise skip first row
if counter<1:
for row in filereader:
writer.writerow(row)
counter+=1
else:
header = next(filereader,None)
for row in filereader:
writer.writerow(row)