python用pandas写入csv_python - 将Pandas DataFrame写入CSV文件_python_酷徒编程知识库

python 2

(其中"df"是你的DataFrame对象。)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

然后尝试:df.to_csv(file_name)

你可以通过以下方式检查列的编码:for column in df.columns:

print \'{0} {1}\'.format(str(type(df[column][0])),str(column))

警告errors =\'ignore\'将省略字符,例如,IN: unicode(\'Regenexxxae\',errors=\'ignore\')

OUT: u\'Regenexx\'

python 3for 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

你可能感兴趣的:(python用pandas写入csv_python - 将Pandas DataFrame写入CSV文件_python_酷徒编程知识库)