用俄文字符写入numpy.ndarray文件

我尝试写numpy.ndarray文件。 我用

 unique1 = np.unique(df['search_term']) unique1 = unique1.tolist() 

然后再试1)

 edf = pd.DataFrame() edf['term'] = unique1 writer = pd.ExcelWriter(r'term.xlsx', engine='xlsxwriter') edf.to_excel(writer) writer.close() 

和2)

 thefile = codecs.open('domain.txt', 'w', encoding='utf-8') for item in unique: thefile.write("%s\n" % item) 

但是,所有返回的UnicodeDecodeError: 'utf8' codec can't decode byte 0xd7 in position 9: invalid continuation byte

如果将string编码为utf8,则第二个示例应该可以工作。

以下工作在Python2中,使用utf8编码的文件:

 # _*_ coding: utf-8 import pandas as pd edf = pd.DataFrame() edf['term'] = ['foo', 'bar', u'русском'] writer = pd.ExcelWriter(r'term.xlsx', engine='xlsxwriter') edf.to_excel(writer) writer.save() 

输出:

在这里输入图像说明