python


Issue with UTF-/ encoding on csv file for excel


EDIT:
As suggested special chars are displayed correctly if I use notepad++ to open the csv file. They are displayed correctly too when I import the csv file into excel.
How can I generate a csv file that is displayed correctly when opened by excel since file importing is not an option for the users
I'm generating a csv file that is being processed using Excel.
Special caracters like 'é' are not displayed properly when the file is opened with excel
This the poc I'm using to generate the csv file
# -*- coding: utf-8 -*-
import unicodecsv as csv
import codecs
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
def write_csv(file,headers):
resultFile =codecs.open(file, "w+", "utf-8")
#headers=[s.encode('utf-8') for s in headers]
wr = csv.writer(resultFile, dialect='excel',delimiter=";",encoding="utf-8")
wr.writerow(headers)
resultFile.close()
headers=[""]
headers.append("Command")
headers.append("Vérification".encode('utf-8'))
write_csv(r"C:\test2.csv",headers)
Python 2 solution using unicodecsv. Note that the documentation for unicodecsv says the module should be opened in binary mode (wb). Make sure to write Unicode strings. #coding is required to support non-ASCII characters in the source file. Make sure to save the source file in UTF-8.
#coding:utf8
import unicodecsv
with open('test.csv','wb') as f:
# Manually encode a BOM, utf-8-sig didn't work with unicodecsv
f.write(u'\ufeff'.encode('utf8'))
w = unicodecsv.writer(f,encoding='utf8')
# Write Unicode strings.
w.writerow([u'English',u'Chinese'])
w.writerow([u'American',u'美国人'])
w.writerow([u'Chinese',u'中国人'])
Python 3 solution. #coding is optional here because it defaults to UTF-8. Just make sure to save the source file in UTF-8. unicodecsv is no longer required. The built-in csv works correctly. csv documentation says to open the file with newline=''.
#coding:utf8
import csv
with open('test.csv','w',newline='',encoding='utf-8-sig') as f:
w = csv.writer(f)
# Write Unicode strings.
w.writerow([u'English',u'Chinese'])
w.writerow([u'American',u'美国人'])
w.writerow([u'Chinese',u'中国人'])
I fixed this issue using UTF-8 BOM encoding.
# -*- coding: utf-8-sig-*-
import unicodecsv as csv
import codecs
import sys
reload(sys)
sys.setdefaultencoding("utf-8-sig")
def write_csv(file,headers):
resultFile =codecs.open(file, "w+", "utf-8-sig")
#headers=[s.encode('utf-8') for s in headers]
wr = csv.writer(resultFile, dialect='excel',delimiter=";",encoding="utf-8-sig")
wr.writerow(headers)
resultFile.close()
headers=[""]
headers.append("Command")
headers.append("Vérification")
write_csv(r"C:\Users\ATHENA-HDA\AppData\Local\Temp\test2.txt",headers)

Related Links

working with datetime: get the difference between dates as an integer
Python face detection error with opencv
How to loop through a set and get the last value of set
Fixing a While loop in a pig latin translator
error with docker-compose and django
Find all tags of certain class only after tag with certain text
How to go up a directory in my shebang line?
Asks user to input floating-point values. If invalid inputs more than twice, quit the program using python
scrapy not printing out stacktrace on exception
Multiple 2D list confusion in Python
Error compiling dlib on os x
Retrieving data from instagram api using python
Python Matplotlib Barplot Questions (xaxis date formatting and colormapping)
Efficiently doing interrow calculations in Pandas
Django project : production server, use python 3
Difference in declaring a function in Python and C

Categories

HOME
cluster-computing
sidekiq
ibm-watson-cognitive
signalr
voip
ncurses
firebase-app-indexing
bower
paramiko
google-contacts
puppet
aem
google-sheets-api
docker-swarm
azureservicebus
webdav
value
android-viewpager
lotus-notes
image-recognition
gwtp
simple-injector
cratedb
line-api
rapidjson
riak
session-timeout
arraylist
grub2
derived
cvs2svn
ghost-inspector
tflearn
iis-10
winscp
reactiveui
multichoiceitems
aurelia-binding
vegan
twitter-bootstrap-2
entity-system
consumer
canvasjs
polymorphism
data-extraction
rhomobile
nsurlconnection
rhino
active-model-serializers
s3cmd
code-composer
lftp
nsjsonserialization
dimple.js
istorage
photon-controller
in-memory-database
emoticons
rxvt
restivejs
typhoon
c64
windows-azure-pack
xib
packagemaker
gherkin
listadapter
mercurial-hook
emokit
flexjson
approval-tests
lmfit
sem
xaml-designer
git-checkout
robocode
moai
rhel5
ultrawingrid
snmp4j
method-overriding
bullet
kraken.js
driver-signing
javascriptmvc
ienumerator
surveyor-gem
objcmongodb
angularjs-timeout
dropdownlistfor
attachevent
bulbs
git-filter-branch
entity-framework-ctp5
opengl-es-lighting
exitstatus
unreachable-code
external-assemblies
gwt-2.2-celltable
google-instant
microsoft.ink
data-execution-prevention

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html