Reputation: 3166
In SPSS, using python, I am writing a list of lists into a csv file:
begin program.
import spss,spssaux, sys, csv, codecs
def WriteDim():
MyArray=[some list of lists]
for MyVar in MyFile.varlist:
MyArray.append([MyVar.name,MyVar.label])
DimFile="DimCSV.csv"
with codecs.open(DimFile, "w",encoding='utf8') as output:
writer = csv.writer(output, lineterminator='\n')
writer.writerows(MyArray)
end program.
I have some Spanish text in my practice array, for example "reparación"
. If I open the output file in a text editor, all looks fine. However, if I open it in Excel 2016, it looks like this: "reparación"
. I would need to go to Data/Import From text" and manually choose UTF encoding, but this is not an option for the future users of my SPSS program.
Is there any way of writing the file so that Excel will open it using UTF-8 encoding ? It has to be a csv file - opening it in excel is only one use of it.
Upvotes: 0
Views: 263
Reputation: 3166
While Serge Ballesta's answer worked perfectly for Spanish, I found that encoding='utf-8-sig'
works best for all characters I tested. I felt UTF-8 should be used, as it is more common than the other suggested encodings.
Credit to this topic: Write to UTF-8 file in Python
Upvotes: 0
Reputation: 148890
You explicitely ask for a utf8 encoding at codecs.open(DimFile, "w",encoding='utf8')
, and later say you would prefere not to use utf8. Just directly use the expected encoding:
with codecs.open(DimFile, "w",encoding='cp1252') as output:
(cp1252 is the common encoding for Spanish on Windows)
Upvotes: 1