2

I want to write a list into a csv file in python. This is a subset of my input list.

rows= ([u'Feng Ming', u'Cao', u'China Samsung Telecom', u'Beijing', u'', u'CHINA'], 
    [u'Naftali', u'Chayat', u'Alvarion Ltd.', u'Tel Aviv', u'', u'ISRAEL'],
    [u'R\xe9mi', u'Chayer', u'Harris Corporation', u'Dollard-Des-Ormeaux', u'PQ', u'CANADA'])

However, when I am using the code

with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(rows)

Its showing error because of the character "R\xe9mi". Any lead to solve this problem will be appreciated. Thanks

1
  • Are you using Python 2.x? Commented Aug 5, 2017 at 12:07

2 Answers 2

1

You have to add import unicodecsv as csv to do this task.

import unicodecsv as csv
rows= [u'Feng Ming', u'Cao', u'China Samsung Telecom', u'Beijing', u'', u'CHINA'], [u'Naftali', u'Chayat', u'Alvarion Ltd.', u'Tel Aviv', u'', u'ISRAEL'], [u'R\xe9mi', u'Chayer', u'Harris Corporation', u'Dollard-Des-Ormeaux', u'PQ', u'CANADA']

with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(rows)

output.csv :

Feng Ming,Cao,China Samsung Telecom,Beijing,,CHINA
Naftali,Chayat,Alvarion Ltd.,Tel Aviv,,ISRAEL
Rémi,Chayer,Harris Corporation,Dollard-Des-Ormeaux,PQ,CANADA
Sign up to request clarification or add additional context in comments.

Comments

0

Just use unicodecsv

pip install unicodecsv

import unicodecsv as csv
# Your code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.