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

Error when using astWCS trying to create WCS object
if there any better way to read bb function souce code.i was very faint
PyQt4 signals and slots
Sql Alchemy What is wrong?
How do I plot a graph in Python?
Reordering matrix elements to reflect column and row clustering in naiive python
making urllib request in Python from the client side
Grid within a frame?
how to create http headers from scratch
Mapping a range of values to another
maya2008 win32api 64 bit python
Python importing modules differently when run interactively/non-interactively
Why do I get this error in this Python code?
what is the correct way to close a socket in python 2.6?
Listing blog entries by year,month
Extract URLs out of email in Python

Categories

HOME
python
vue.js
signalr
system-verilog
raspbian
bing
fparsec
json-ld
label
google-spreadsheet-api
metronic
x11
pjsip
nixos
gatsby
dryioc
symfony-forms
session-timeout
jogl
eclipselink
scalaz7
extractor
restfb
printdocument
sumo
guile
frp
helper
forum
superagent
dhtmlx-scheduler
jackson-dataformat-csv
xor
apache-cloudstack
nodeclipse
swagger-editor
fax
alchemy.js
twiki
common.logging
ssh.net
dwarf
fedora20
tofixed
createobject
settimeout
hspec
tomee
manifest.mf
chessboard.js
aescryptoserviceprovider
mix
soda
financial
anti-cheat
interactive-brokers
java.util.calendar
file-diffs
windows-vista
ninject.web.mvc
twython
sparse-file
swift2.1
web-component-tester
consensus
obfuscar
moai
named-ranges
left-recursion
mser
config-files
magic-numbers
pyjade
bigint
fogbugz-api
goinstant
dynamic-binding
dynamics-ax-2009
enumerators
linkedhashset
css-tables
vim-powerline
radchart
responsetext
entity-framework-ctp5
flexicious
wchar
zookeeper
easygui
charts4j
gethashcode
svn-hooks
recordset
quick-search
surefire
pci-bus
iphone-sdk-3.2
scatterview
hmacsha1
database-diagramming
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
Mobile App
Mobile App
Mobile App