dcraven
dcraven

Reputation: 139

UnicodeEncodeError with character u'\u2013

I am receiving the below error with the dash character "-"

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

I have tried using the following: skills.encode('utf-8') but I still get the error. Below is my code in which I am trying to write to csv.

 writer.writerow([name.encode('utf-8'),
                 heading.encode('utf-8'),
                 location.encode('utf-8'),
                 education.encode('utf-8'),
                 summary,
                 currentRole,
                 allRoles,
                 companiesFollowed,
                 groups,
                 skills.encode('utf-8')])

Upvotes: 0

Views: 1482

Answers (1)

Adam Smith
Adam Smith

Reputation: 54223

You can specify one of a number of settings to str.encode under the errors keyword. More info can be found in the docs but I'd recommend you use the 'replace' error handler.

writer.writerow([name.encode('utf-8', errors='replace'),
    heading.encode('utf-8', errors='replace'),
    location.encode('utf-8', errors='replace'),
    education.encode('utf-8', errors='replace'),
    summary,
    currentRole,
    allRoles,
    companiesFollowed,
    groups,
    skills.encode('utf-8', errors='replace')])

This will end up making a bytes object with a ? in place of each unencodable code point.

Upvotes: 1

Related Questions