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
43 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
93 views
0 votes
0 answers

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

for i in cnames:     print(i)     df_csv[i] = (df_csv[i] - ...READ MORE

3 days ago in Python by anonymous
8 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

16 hours ago in Python by Waseem
• 3,400 points
2 views
+2 votes
2 answers

Error while printing hello world in python.

You must be trying this command in ...READ MORE

answered Mar 31, 2018 in Python by GandalfDwhite
• 1,320 points
61 views
+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 125 views
+4 votes
6 answers