如何在不收到Unicode错误的情况下写入CSV?

时间:2014-06-03 20:44:10

标签: python csv unicode

我已经看过很多关于这个问题的例子,但还没有找到一个直截了当的解决方案,对我有用。我仍然收到错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xad' in position 5: ordinal not in range(128)

这是我根据stackoverflow提出的类似问题汇总的代码。

f = open(out_filepath, 'w')
  writer = csv.DictWriter(f, fieldnames, extrasaction='ignore')
  headers = dict([(header, header) for header in fieldnames])
  data = [headers]
  data.extend([row for row in rows]) # add data rows
  for row in data:
    try:
        writer.writerow(row)
    except:
        for value in row.itervalues():
            for s in value:
                try:
                    unicode(s).encode("utf-8")
                except:
                    s = ''
        writer.writerow(row)
  f.close() 

以下是我尝试过的更新代码,但仍然给我错误:

for row in data:
    try:
        writer.writerow(row)
    except:
        for key in row:
            value = row[key]
            letterlist = list(value)
            for i in range(len(letterlist)):
                try:
                    letterlist[i].decode('string_escape')
                    letterlist[i].encode('ascii', 'ignore')
                except:
                    print 'Letter excluded from ' +key+' '+ str(letterlist) 
                    letterlist[i] = ''
            value = ''.join(letterlist)
            row[key] = value
        #print row
        writer.writerow(row)

2 个答案:

答案 0 :(得分:1)

以下是对我有用的内容:

f = open('eg.csv', 'w')
s = 'some troublesome string'
f.write(s.decode('string_escape')) 

如果这不起作用,我会这样做:

f.write(s.encode('ascii', 'ignore'))

答案 1 :(得分:0)

我最终使用此函数将unicode转换为utf8。

def ValConvert(val):
  if type(val).__name__ == 'unicode':
    return val.encode('utf8')
  elif type(val).__name__ == 'str':
    return val
  else:
    return str(val)