python - csv dictReader encoding not correct -


i have following script read utf-8 csv:

def readcsv(f, bdgs): open(f) csvfile:     reader = csv.dictreader(csvfile, delimiter=';')     row in reader:         key, val  in row.iteritems():             print type(key), key,':',type(val),val         print type(row), row         if row['objectid'] not '':             # magic 

which yield :

processing following files: ['fenetre.csv'] <type 'str'> type de fenêtre_uniqueid : <type 'str'> uid-100 <type 'str'> type de fenêtre_checkdelete : <type 'str'>  <type 'str'> type de fenêtre_nom : <type 'str'> fenetre 2006-2010 <type 'str'> type de fenêtre_intercalaire : <type 'str'> 1 <type 'str'> liste des fenêtres_hauteur : <type 'str'> 3.29 <type 'str'> objectid : <type 'str'> 3760 <type 'str'> liste des fenêtres_nb vantaux : <type 'str'> 2 <type 'str'> liste des fenêtres_façade : <type 'str'> uid-001-aw1 <type 'str'> type de fenêtre_cadre : <type 'str'> 7 <type 'str'> type de fenêtre_vitrage : <type 'str'> 4 <type 'str'> liste des fenêtres_part cadre : <type 'str'> 20 <type 'str'> liste des fenêtres_nom : <type 'str'> f1 <type 'str'> liste des fenêtres_nombre : <type 'str'> 1 <type 'str'> liste des fenêtres_ombrage1 : <type 'str'> uid-201 <type 'str'> liste des fenêtres_largeur : <type 'str'> 1.55 <type 'str'> liste des fenêtres_ombrage2 : <type 'str'>  <type 'str'> liste des fenêtres_checkdelete : <type 'str'>  <type 'str'> liste des fenêtres_type de fenêtre : <type 'str'> uid-100 <type 'dict'> {'type de fen\xc3\xaatre_uniqueid': 'uid-100', 'type de fen\xc3\xaatre_checkdelete': '', 'type de fen\xc3\xaatre_nom': 'fenetre 2006-2010', 'type de fen\xc3\xaatre_intercalaire': '1', 'liste des fen\xc3\xaatres_hauteur': '3.29', '\xef\xbb\xbfobjectid': '3760', 'liste des fen\xc3\xaatres_nb vantaux': '2', 'liste des fen\xc3\xaatres_fa\xc3\xa7ade': 'uid-001-aw1', 'type de fen\xc3\xaatre_cadre': '7', 'type de fen\xc3\xaatre_vitrage': '4', 'liste des fen\xc3\xaatres_part cadre': '20', 'liste des fen\xc3\xaatres_nom': 'f1', 'liste des fen\xc3\xaatres_nombre': '1', 'liste des fen\xc3\xaatres_ombrage1': 'uid-201', 'liste des fen\xc3\xaatres_largeur': '1.55', 'liste des fen\xc3\xaatres_ombrage2': '', 'liste des fen\xc3\xaatres_checkdelete': '', 'liste des fen\xc3\xaatres_type de fen\xc3\xaatre': 'uid-100'} traceback (most recent call last):   file "./oba.py", line 120, in <module>     sys.exit(main())   file "./oba.py", line 115, in main     readcsv(f,out)   file "./oba.py", line 37, in readcsv     if row['objectid'] not '': keyerror: 'objectid' 

if @ last line before stack trace, see although encoding key , values strings in first row correct. dict not store key/values proper encoding. hence error.

in order fix issue, tried this:

def unicodedictreader(utf8_data, **kwargs):     csv_reader = csv.dictreader(utf8_data, **kwargs)     row in csv_reader:         yield {unicode(key, 'utf-8') : unicode(value, 'utf-8') key, value in row.iteritems()}  def readcsv(f, bdgs):     js=getjsonmap()     open(f) csvfile:         reader = unicodedictreader(csvfile, delimiter=';')         row in reader:             key, val  in row.iteritems():                 print type(key), key,':',type(val),val             print type(row), row             if row['objectid'] not '': 

which yield this:

<type 'unicode'> type de fenêtre_cadre : <type 'unicode'> 7 <type 'unicode'> liste des fenêtres_hauteur : <type 'unicode'> 3.29 <type 'unicode'> type de fenêtre_uniqueid : <type 'unicode'> uid-100 <type 'unicode'> liste des fenêtres_nom : <type 'unicode'> f1 <type 'unicode'> objectid : <type 'unicode'> 3760 <type 'unicode'> type de fenêtre_intercalaire : <type 'unicode'> 1 <type 'unicode'> liste des fenêtres_ombrage1 : <type 'unicode'> uid-201 <type 'unicode'> liste des fenêtres_largeur : <type 'unicode'> 1.55 <type 'unicode'> liste des fenêtres_part cadre : <type 'unicode'> 20 <type 'unicode'> liste des fenêtres_type de fenêtre : <type 'unicode'> uid-100 <type 'unicode'> type de fenêtre_nom : <type 'unicode'> fenetre 2006-2010 <type 'unicode'> liste des fenêtres_checkdelete : <type 'unicode'>  <type 'unicode'> liste des fenêtres_nb vantaux : <type 'unicode'> 2 <type 'unicode'> type de fenêtre_checkdelete : <type 'unicode'>  <type 'unicode'> type de fenêtre_vitrage : <type 'unicode'> 4 <type 'unicode'> liste des fenêtres_façade : <type 'unicode'> uid-001-aw1 <type 'unicode'> liste des fenêtres_ombrage2 : <type 'unicode'>  <type 'unicode'> liste des fenêtres_nombre : <type 'unicode'> 1 <type 'dict'> {u'type de fen\xeatre_cadre': u'7', u'liste des fen\xeatres_hauteur': u'3.29', u'type de fen\xeatre_uniqueid': u'uid-100', u'liste des fen\xeatres_nom': u'f1', u'\ufeffobjectid': u'3760', u'type de fen\xeatre_intercalaire': u'1', u'liste des fen\xeatres_ombrage1': u'uid-201', u'liste des fen\xeatres_largeur': u'1.55', u'liste des fen\xeatres_part cadre': u'20', u'liste des fen\xeatres_type de fen\xeatre': u'uid-100', u'type de fen\xeatre_nom': u'fenetre 2006-2010', u'liste des fen\xeatres_checkdelete': u'', u'liste des fen\xeatres_nb vantaux': u'2', u'type de fen\xeatre_checkdelete': u'', u'type de fen\xeatre_vitrage': u'4', u'liste des fen\xeatres_fa\xe7ade': u'uid-001-aw1', u'liste des fen\xeatres_ombrage2': u'', u'liste des fen\xeatres_nombre': u'1'} traceback (most recent call last):   file "./oba.py", line 120, in <module>     sys.exit(main())   file "./oba.py", line 115, in main     readcsv(f,out)   file "./oba.py", line 37, in readcsv     if row['objectid'] not '': keyerror: 'objectid' 

which makes me perplex happening behind scenes encoding:

  • how fix without having "\ufeffobjectid" in dict?
  • why in second attempt python acknowledging reads utf-8 (unicode) data in row still displays wrong way when print row dict?
  • is problem printing/storing unicode dicts in python ? behavior expected container?

can more insight on encoding give me info on happening behind scenes here ?

thanks.

edit: worth mentionning in header of file have declared encoding utf-8. (i.e. "# -- coding: utf-8 -- "), , running v2.7.6

how fix without having "\ufeffobjectid" in dict?

use utf-8-sig instead of utf-8 decode. automatically removes bom codepoint when decoding utf-8-encoded byte string.

why in second attempt python acknowledging reads utf-8 (unicode) data in row still displays wrong way when print row dict?

printing containers uses repr() when printing container items. can see actual data in strings. print container items directly see "pretty" version.

is problem printing/storing unicode dicts in python ?

it's not problem. it's display method. data in strings same.

is behavior expected container?

yes.

also, not use is test empty string. use:

if row['objectid'] != '': 

or better, since empty strings considered false:

if row['objectid']: 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -