嗨,我注意到,在使用DictWriter
和delimiter=' '
而不是字符串时,通过使用逗号而不','
将字符串保存到文件中。""
如何设置没有字符串" "
?
代码
import csv
golds_two =[]
mydict ={}
with open ('fileA.csv','rU') as csvfile:
wszystkie=csv.DictReader(csvfile,delimiter=',')
for w in wszystkie:
mydict[(w['URL']).split('/')[-1]]=w['MediaObject ID']
with open ('fileB.csv','rU') as csvfile:
golds=csv.DictReader(csvfile,delimiter=';')
for g in golds:
g['MediaObject ID']=mydict[g['ID']]
golds_two.append(g)
with open('fileC.csv','w') as F:
head_fields =golds_two[0].keys()
head_fields.remove('ID')
print head_fields
head_fields=sorted(head_fields,reverse=True)
csvdw = csv.DictWriter(F,delimiter=" ",fieldnames=head_fields)
headers = dict( (n,n) for n in head_fields)
for z in golds_two:
z.pop('ID',None)
print z
csvdw.writerow(z)
文件A.csv:
MediaObject ID,URL
1152901,http://foo.bar/TRU716565.jpg
1152902,http://foo.bar/TRU716566.jpg
文件B.csv:
ID;Gold label
TRU716565.jpg;Identifable X
TRU716566.jpg;Non identfiable X
结果文件C.csv:
1152901 "Identifable X"
1152902 "Non identfiable X"