python - csv.DictWriter delimiter set to space implies text in " " -
hi noticed while using dictwriter , delimiter=' ' instead of ',' string saved file in "" while use of comma without. how set things have strings without " "?
code
import csv golds_two =[] mydict ={} open ('filea.csv','ru') csvfile: wszystkie=csv.dictreader(csvfile,delimiter=',') w in wszystkie: mydict[(w['url']).split('/')[-1]]=w['mediaobject id'] open ('fileb.csv','ru') csvfile: golds=csv.dictreader(csvfile,delimiter=';') g in golds: g['mediaobject id']=mydict[g['id']] golds_two.append(g) open('filec.csv','w') 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) n in head_fields) z in golds_two: z.pop('id',none) print z csvdw.writerow(z) filea.csv:
mediaobject id,url 1152901,http://foo.bar/tru716565.jpg 1152902,http://foo.bar/tru716566.jpg fileb.csv:
id;gold label tru716565.jpg;identifable x tru716566.jpg;non identfiable x resulting filec.csv:
1152901 "identifable x" 1152902 "non identfiable x"
normally, suppress quoting of data fields in csv file need add aquoting=csv.quote_nonekeyword argument csv.dictwriter()constructor call.
however, doing in case cause exception raised because of data fields contain thedemilitercharacter trying use (' '), , differentescapecharwas not defined. mentioned in documentation says:
if escapechar not set, writer raise error if characters require escaping encountered.
this makes total sense if think -- csv file unparsable (i.e. invalid) if data in fields contained unescaped delimiter characters because there no way tell data delimiters -- they'd non-identifiable -- if results looked this:
1152901 identifable x 1152902 non identfiable x so changing delimiter to"\t"will rid of quotes (as discovered), or alternatively define anescapechar="\\"(or whatever) in constructor call , leave delimiter set to" "-- because either there no longer conflict between file's delimiter , contents of data fields or there way escape them when they're encountered.
if want results way stated unknown reason, can write file out manually, suppose.
update: here's 1 possible workaround changes space characters in data fields to'\xa0' non-breaking space on system displays same regular space character.
with open('filec.csv','w') 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, quoting=csv.quote_none) headers = dict( (n,n) n in head_fields) z in golds_two: z.pop('id',none) print z k,v in z.iteritems(): z[k] = v.replace(' ', '\xa0') csvdw.writerow(z)
Comments
Post a Comment