Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:20:54+00:00 2026-06-14T18:20:54+00:00

I’m getting this error: UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 52-57: ordinal

  • 0

I’m getting this error:

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 52-57: ordinal not in range(128)

The code causing the error:

f.write(textwrap.dedent(unicode(the_string))

The string I’m trying to write is an excerpt from a website which contains English and Japanese (for testing) Transliteration of foreign words and names, such as コンピュータ (konpyūta, “computer”) and ロンドン (Rondon, “London”). (Some foreign borrowings that have become naturalized may not be rendered in katakana.)

When handling a string in Python that is In another language (Japanese). How can I parse this to prevent the error and still retain the string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:20:56+00:00Added an answer on June 14, 2026 at 6:20 pm

    The problem here is that the .write method of the file object is naively trying to convert the unicode string (if you are in Python 2.x, this will be of the unicode type) that you pass it to a byte string (if you are in Python 2.x, this will be of the str type) using the ASCII codec, but the unicode string you are passing cannot be represented using ASCII because it has (Japanese) characters that are not part of the ASCII character set.

    You need to use the .encode method of the unicode string to convert it to a series of bytes that represent that string before you can save it. That’s basically what the str type represents in Python 2.x – just a series of bytes, not a series of characters like you might expect. Python can easily make you think otherwise, though, because when you print a variable of type str, Python displays it – as a series of characters in your terminal – using the system’s default unicode encoding.

    What encoding you should use here to encode your string depends upon your use case. UTF-8 is the most common and you may simply want to use that, but if you want to make sure that the file you are writing to will display properly in a text editor on the same system even if you run it on a device with a less usual system encoding like UTF-16, you may instead want to use the default encoding of the system (which will of course fail if the system has a default encoding that cannot encode your string).

    In other words, you almost certainly want to do one of the following things:

    a)

    f.write(textwrap.dedent(the_string).encode('utf-8'))

    b)

    import sys
    f.write(textwrap.dedent(the_string).encode(sys.getdefaultencoding()))
    

    If you think that this is a pretty irritating and complicated load of stuff to get your head around to perform the fairly basic task of writing some non-ASCII text to a file, then – I agree with you! When I started with Python – which was my first programming language – I struggled a lot with understanding unicode, string encoding, and the Python types and methods that relate to it. However, the complexity is not Python’s fault – it’s down to the way that computers encode text, and specifically the fact that there are multiple encodings for text. Different series of bytes can represent the same series of characters, depending upon the encoding that is being used. This makes it impossible for Python to just conceal the nuts and bolts of string encoding from you and ‘automatically do something sensible’ like I, as a rookie, naively hoped and expected it would.

    If you’re going to be writing any significant amount of code involving getting and using text data from the web that may contain non-ASCII characters, I’d recommend reading around this topic thoroughly and getting a solid understanding of it, both from a general and Python-specific perspective.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.