写numpy.ndarray公司把俄文字符改成fi

2024-06-02 05:57:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

下次再试试 (一)

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


Tags: 文件innumpydfnpbyteitemwriter
1条回答
网友
1楼 · 发布于 2024-06-02 05:57:50

如果将字符串编码为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

相关问题 更多 >