Python Error "UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128)"

0 votes

I'm trying to write a to a csv file, I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128)

Line causing error:

df.to_csv('out.csv')
Jun 13 in Python by Kamal
1,002 views

2 answers to this question.

0 votes

use the sep argument of to_csv, to delimit by a tab:

df.to_csv(file_name, sep='\t')

To use a specific encoding (e.g. 'utf-8') use the encoding argument:

df.to_csv(file_name, sep='\t', encoding='utf-8')
answered Jun 13 by Rhea
0 votes
import csv

import sys
reload(sys)
sys.setdefaultencoding('utf8')

data = [["a", "b", u'\xe9']]

with open("output.csv", "w") as csv_file:
    writer = csv.writer(csv_file, quoting=csv.QUOTE_ALL)
    writer.writerows(data)
answered Jun 28 by anonymous

Hi. I am getting the following error when I ran this script:

NameError: name 'reload' is not defined

Use the following line to import the module

from importlib import reload

In Python 2, this was in build but in python, you've to use importlib.

Related Questions In Python

0 votes
2 answers

Error: Speech to Text Codec cannot decode the bytes in position

import speech_recognition as sr r = sr.Recognizer() audio ='C\Users\Desktop\audiofile1.wav' with ...READ MORE

answered Nov 27, 2018 in Python by Nabarupa Das
207 views
0 votes
1 answer

i am normalizing the data set iris in python and get the error ::TypeError: 'numpy.float64' object is not callable

TRY THIS   #Nomalisation for i in names:     print(i)   ...READ MORE

answered Aug 19 in Python by Noel Deepak Palle
610 views
0 votes
0 answers

How to get the ASCII values of a character in python?

Is there a function for this task ...READ MORE

Jul 17 in Python by Waseem
• 4,480 points
35 views
+1 vote
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 405 views
+4 votes
6 answers