BeatJuice
BeatJuice

Reputation: 49

Exporting arabic words to csv in Python 3

I'm trying to export some arabic in a csv file after getting it from the translater. I Always run into a problem when trying to write it in the CSV. The problem is this one :

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>

My code goes as follow (a bit of it, trying to be concise here) :

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()

When I build it in SublimeText with a print instead of the csvwriter, I get the result without problem. It's only when I want to write in the csv that I get the issue. Any ideas on how to fix this?

I've seen Something about encoding or decoding it in UTF-8, but I don't know where to add this possibility

Thanks!

Upvotes: 0

Views: 1036

Answers (1)

Ali Kargar
Ali Kargar

Reputation: 189

you can use UTF-8 by declaring it at the top of your code :

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

then you can use utf-8 encoding/decoding.
UPDATE: Based on these answers you should change the other environments Unicode like your console.
[answer-1][1]
[answer-2][2]

try opening your file with UTF-8 encoding :

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

Upvotes: 1

Related Questions