2016-07-08 30 views
2

我尝试写入文件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

+0

你的意思的标题是*写numpy.ndarray有** **俄罗斯字符到文件*?目前,有一个* u *缺失,这使得通过仅查看标题很难理解您要求的内容。 –

回答

0

第二个例子,如果你编码应该工作字符串为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() 

输出:

enter image description here