2014-01-13 4 views
0

내 데이터 프레임의 문자열에 \xc9 액센트가 있으면 Pandas의 to_csv() 메서드에서 오류가 발생합니다. 어떤 생각을 어떻게 빨리 풀 수 있습니까? xc9 데이터 프레임을 CSV로 내보낼 때

감사

--------------------------------------------------------------------------- UnicodeEncodeError Traceback (most recent call last) /Users/slegroux1/Projects/FeaturEmo/en_features.py in() 360 print "----- No plot. Corresponding features are missing." 361 if name == 'main': --> 362 main()

/Users/slegroux1/Projects/FeaturEmo/en_features.py in main() 40 if output: 41 embed() ---> 42 dict2frame(features).to_csv(output) 43 if plot and not(directory or my_csv_list): 44 plot_features(features)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/frame.pyc in to_csv(self, path_or_buf, sep, na_rep, float_format, cols, header, index, index_label, mode, nanRep, encoding, quoting, line_terminator, chunksize, tupleize_cols, **kwds) 1408
chunksize=chunksize,engine=kwds.get("engine"), 1409
tupleize_cols=tupleize_cols) -> 1410 formatter.save() 1411 1412 def to_excel(self, excel_writer, sheet_name='sheet1', na_rep='',

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/format.pyc in save(self) 970 971 else: --> 972 self._save() 973 974

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/format.pyc in _save(self) 1076 break 1077 -> 1078 self._save_chunk(start_i, end_i) 1079 1080 def _save_chunk(self, start_i, end_i):

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/format.pyc in _save_chunk(self, start_i, end_i) 1094 ix = data_index.to_native_types(slicer=slicer, na_rep=self.na_rep, float_format=self.float_format) 1095 -> 1096 lib.write_csv_rows(self.data, ix, self.nlevels, self.cols, self.writer) 1097 1098 # from collections import namedtuple

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/lib.so in pandas.lib.write_csv_rows (pandas/lib.c:13871)()

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

답변

1

당신은 to_csvencoding 인수를 사용하여 인코딩을 지정해야합니다. 예 : df.to_csv(filename, encoding='utf-8').

관련 문제