在 Python 3 中将阿拉伯语单词导出到 csv

Exporting arabic words to csv in Python 3

我试图在从翻译器获取一些阿拉伯语后将其导出到 csv 文件中。尝试将其写入 CSV 时,我总是 运行 遇到问题。问题是这个:

return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 2-7: character maps to <undefined>

我的代码如下(其中一些,尽量在这里简明扼要):

from bs4 import BeautifulSoup
import requests
import csv
from yandex_translate import YandexTranslate


csv_file = open("syno.csv", "w", newline = '')
csv_writer = csv.writer(csv_file)

 #making the request to the translater and so on, not written here, tell me if you need it but I don't think so. 

traduction =(translate.translate('bonjour', 'fr-ar'))

csv_writer.writerow([traduction["text"]])
csv_file.close()

当我在 SublimeText 中使用打印而不是 csvwriter 构建它时,我得到的结果没有问题。 只有当我想在 csv 中写入时,我才会遇到问题。有想法该怎么解决这个吗?

我看过一些关于用 UTF-8 编码或解码它的东西,但我不知道在哪里添加这种可能性

谢谢!

您可以通过在代码顶部声明来使用 UTF-8:

# -*- coding: <encoding name> -*-

那么你可以使用utf-8 encoding/decoding.
更新:根据这些答案,您应该像控制台一样更改其他环境 Unicode。
[答案-1][1]
[答案-2][2]

尝试使用 UTF-8 编码打开文件:

csv_file = open("syno.csv", "w", encoding='utf-8', newline = '')