Here is my code
The following works but writes the wrong string
import csv
import codecs
if __name__ == "__main__":
# This works for writing unico but writes wrong string
with codecs.open("./why_unicode.csv", "wb" ) as csv_file:
writer = csv.writer(csv_file)
unico = u'IP\u4e13\u7528\u8033\u673a.\u9ed1'.encode('utf-8')
writer.writerow(unico)
Here is the result
I,P,ä,¸,“,ç,”,¨,è,€,³,æ,œ,º,.,é,»,‘
which is not the correct string. The correct string is 'IP专用耳机.黑'
This doesn't work
import csv
import codecs
if __name__ == "__main__":
with codecs.open("./why_unicode.csv", "wb", 'utf-8' ) as csv_file:
writer = csv.writer(csv_file)
unico = u'IP\u4e13\u7528\u8033\u673a.\u9ed1'
writer.writerow(unico)
Here is the error
SyntaxError: Non-ASCII character '\xe4' in file
test_unicode.py on line 15, but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
And this won't run at all
import csv
import codecs
if __name__ == "__main__":
with codecs.open("./why_unicode.csv", "wb") as csv_file:
writer = csv.writer(csv_file)
unico = u'IP\u4e13\u7528\u8033\u673a.\u9ed1'.encode('utf-8')
#chn = u'IP专用耳机.黑' # even commenting out will return error
writer.writerow(unico)
The standard response to this type of question in stackoverflow is to either use codecs
or to encode('utf-8
), I tried both but neither works, this is a bit confusing, can someone help me out?
Edit:
The script is using python 2.7.3 (from python -V)