4

I have this problem when trying to write a csv file :

from csv import writer

str1 = "idzie wąż wąską dróżką"
str2 = "мир и любовь"

csv_file = open("myFile.csv", "a", newline="", encoding="utf-8")

writer_Obj = writer(csv_file,  delimiter=';')    

writer_Obj.writerow([str1, str2])

Here is the result in the file :

idzie wąż wąską dróżką
мир и любовь
Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55
eden clarck
  • 182
  • 2
  • 10
  • 1
    How are you displaying the file? If on Windows some editors assume a local encoding instead of UTF-8 unless a BOM signature is written at the start of the file. Try `encoding='utf-8-sig'` instead. – Mark Tolonen May 27 '18 at 07:43
  • Make sure to delete the existing file before switching to `encoding='utf-8-sig'` as well, since you are opening with append you have to start with a fresh file. – Mark Tolonen May 27 '18 at 07:50
  • I ran your script in Linux (python 3.6.5) and it works as expected. Is your script saved as uft-8? – gbajson May 30 '18 at 11:41
  • @gbajson yes the problem was when I open the csv file with excel it was not in utf-8 but I have solve il by exporting the csv file to xls using excel and its work – eden clarck Jun 01 '18 at 03:00

1 Answers1

0

I have resolved my problem, it was because the csv doent support utf-8 here is an alternative :

www.itg.ias.edu/content/how-import-csv-file-uses-utf-8-character-encoding-0

eden clarck
  • 182
  • 2
  • 10
  • I wouldn't describe it as "csv doesn't support utf-8". A csv file is a specialized text file. All text files use a character encoding. Programs reading a text file have to be told which that is. Problems with expectations arise when programs guess. The linked article describes how to read any text file with Excel. And, in the case of csv , how to tell it all the other metadata that has to come along with a csv file, including field separator and column data types. – Tom Blodget May 27 '18 at 14:04