Một số thứ khác bạn có thể thử nếu bạn gặp sự cố khi mã hóa thành 'utf-8' và muốn di chuyển ô theo ô, bạn có thể thử cách sau.
Python 2
(ở đâu "df" là đối tượng DataFrame của bạn.)
for column in df.columns:
for idx in df[column].index:
x = df.get_value(idx,column)
try:
x = unicode(x.encode('utf-8','ignore'),errors ='ignore') if type(x) == unicode else unicode(str(x),errors='ignore')
df.set_value(idx,column,x)
except Exception:
print 'encoding error: {0} {1}'.format(idx,column)
df.set_value(idx,column,'')
continue
Sau đó thử:
df.to_csv(file_name)
Bạn có thể kiểm tra mã hóa của các cột theo:
for column in df.columns:
print '{0} {1}'.format(str(type(df[column][0])),str(column))
Cảnh báo: errors = 'ignore' sẽ bỏ qua ký tự, ví dụ:
IN: unicode('Regenexx\xae',errors='ignore')
OUT: u'Regenexx'
Python 3
for column in df.columns:
for idx in df[column].index:
x = df.get_value(idx,column)
try:
x = x if type(x) == str else str(x).encode('utf-8','ignore').decode('utf-8','ignore')
df.set_value(idx,column,x)
except Exception:
print('encoding error: {0} {1}'.format(idx,column))
df.set_value(idx,column,'')
continue
Nguồn
2017-01-18 21:00:59
'encoding = 'utf-8'' – Private
Bạn cũng có thể sử dụng 'df.toCSV (file_name, tháng chín =' \ t')', vv – Blairg23
@ Blairg23 Tôi nghĩ đây là tên cũ của chức năng này và đã giảm giá, trên thực tế - điều này không còn có sẵn trong phiên bản mới hơn của gấu trúc! (Tôi không chắc chắn khi nào nó đã được gỡ bỏ ... lạ lùng có vẻ như to_csv đã được sử dụng ở mức 0,4! Http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#id45 Lưu ý: chúng tôi bây giờ là trên 0.14.1) –