Yves
Yves

Reputation: 43

Python 2 export csv with japanese test

import csv
with open('eggs.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

    test_jp = u'\u30a2\u30af\u30bb\u30b5\u30ea\u30fc'
    print(test_jp)
    print(type(test_jp))
    print(repr(test_jp))
    print('-------------------')
    print(test_jp.encode('utf-8'))    
    print(test_jp.encode('cp932'))

    '''
    print(test_jp.decode('utf-8'))
    spamwriter.writerow(test_jp)

    Causeing ERROR
    UnicodeEncodeError: 'ascii' codec can't encode characters
    in position 0-5: ordinal not in range(128)
    '''

I have tried spamwriter.writerow(test_jp.encode('utf-8')).

But the output is garbled -> 'アクセサリー'.

I want the output csv content is 'アクセサリー'

How should I do? (spamwriter.writerow(test_jp) doesn't work)

Upvotes: 1

Views: 1125

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

You need to wrap it in a list for writerow then test_jp.encode("utf-8") will work, writerow expects an iterable so it is iterating over the string writing each byte:

spamwriter.writerow([test_jp.encode("utf-8")])

You can see when we iterate over it we also get strange output:

In [6]: for ch in test_jp.encode("utf-8"):
              print ch
   ...:     

�
�

�
�

�
�

�
�

�
�

�
�


In [7]: print test_jp.encode("utf-8")
アクセサリー

Tested and working:

$ cat eggs.csv 
Spam |Lovely Spam| |Wonderful Spam|
アクセサリー

Upvotes: 1

Related Questions