Exporting arabic words to csv in Python 3











up vote
0
down vote

favorite












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!










share|improve this question






















  • Write file encoding goes in the open statement for that file: docs.python.org/3/howto/…
    – usr2564301
    Nov 13 at 12:20















up vote
0
down vote

favorite












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!










share|improve this question






















  • Write file encoding goes in the open statement for that file: docs.python.org/3/howto/…
    – usr2564301
    Nov 13 at 12:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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!










share|improve this question













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!







python csv arabic writer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 11:46









BeatJuice

223




223












  • Write file encoding goes in the open statement for that file: docs.python.org/3/howto/…
    – usr2564301
    Nov 13 at 12:20


















  • Write file encoding goes in the open statement for that file: docs.python.org/3/howto/…
    – usr2564301
    Nov 13 at 12:20
















Write file encoding goes in the open statement for that file: docs.python.org/3/howto/…
– usr2564301
Nov 13 at 12:20




Write file encoding goes in the open statement for that file: docs.python.org/3/howto/…
– usr2564301
Nov 13 at 12:20












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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 = '')





share|improve this answer



















  • 1




    I think the problem is not in Python, since it's working fine when I build it without writing in the csv.
    – BeatJuice
    Nov 13 at 12:12










  • It's working, i was setting encoding("utf-8") instead of the = sign. Thank you!
    – BeatJuice
    Nov 13 at 12:43













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280373%2fexporting-arabic-words-to-csv-in-python-3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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 = '')





share|improve this answer



















  • 1




    I think the problem is not in Python, since it's working fine when I build it without writing in the csv.
    – BeatJuice
    Nov 13 at 12:12










  • It's working, i was setting encoding("utf-8") instead of the = sign. Thank you!
    – BeatJuice
    Nov 13 at 12:43

















up vote
1
down vote



accepted










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 = '')





share|improve this answer



















  • 1




    I think the problem is not in Python, since it's working fine when I build it without writing in the csv.
    – BeatJuice
    Nov 13 at 12:12










  • It's working, i was setting encoding("utf-8") instead of the = sign. Thank you!
    – BeatJuice
    Nov 13 at 12:43















up vote
1
down vote



accepted







up vote
1
down vote



accepted






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 = '')





share|improve this answer














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 = '')






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 12:33

























answered Nov 13 at 12:05









Ali Kargar

1444




1444








  • 1




    I think the problem is not in Python, since it's working fine when I build it without writing in the csv.
    – BeatJuice
    Nov 13 at 12:12










  • It's working, i was setting encoding("utf-8") instead of the = sign. Thank you!
    – BeatJuice
    Nov 13 at 12:43
















  • 1




    I think the problem is not in Python, since it's working fine when I build it without writing in the csv.
    – BeatJuice
    Nov 13 at 12:12










  • It's working, i was setting encoding("utf-8") instead of the = sign. Thank you!
    – BeatJuice
    Nov 13 at 12:43










1




1




I think the problem is not in Python, since it's working fine when I build it without writing in the csv.
– BeatJuice
Nov 13 at 12:12




I think the problem is not in Python, since it's working fine when I build it without writing in the csv.
– BeatJuice
Nov 13 at 12:12












It's working, i was setting encoding("utf-8") instead of the = sign. Thank you!
– BeatJuice
Nov 13 at 12:43






It's working, i was setting encoding("utf-8") instead of the = sign. Thank you!
– BeatJuice
Nov 13 at 12:43




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280373%2fexporting-arabic-words-to-csv-in-python-3%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?