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

  • Home
  • SEARCH
  • 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 771169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:36:38+00:00 2026-05-14T18:36:38+00:00

I need to change some characters that are not ASCII to ‘_’. For example,

  • 0

I need to change some characters that are not ASCII to ‘_’.
For example,

Tannh‰user -> Tannh_user
  • If I use regular expression with Python, how can I do this?
  • Is there better way to do this not using RE?
  • 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-05-14T18:36:39+00:00Added an answer on May 14, 2026 at 6:36 pm

    Updated for Python 3:

    >>> 'Tannh‰user'.encode().decode('ascii', 'replace').replace(u'\ufffd', '_')
    'Tannh___user'
    

    First we create byte string using encode() – it uses UTF-8 codec by default. If you have byte string then of course skip this encode step.
    Then we convert it to “normal” string using the ascii codec.

    This uses the property of UTF-8 that all non-ascii characters are encoded as sequence of bytes with value >= 0x80.


    Original answer – for Python 2:

    How to do it using built-in str.decode method:

    >>> 'Tannh‰user'.decode('ascii', 'replace').replace(u'\ufffd', '_')
    u'Tannh___user'
    

    (You get unicode string, so convert it to str if you need.)

    You can also convert unicode to str, so one non-ASCII character is replaced by ASCII one. But the problem is that unicode.encode with replace translates non-ASCII characters into '?', so you don’t know if the question mark was there already before; see solution from Ignacio Vazquez-Abrams.


    Another way, using ord() and comparing value of each character if it fits in ASCII range (0-127) – this works for unicode strings and for str in utf-8, latin and some other encodings:

    >>> s = 'Tannh‰user' # or u'Tannh‰user' in Python 2
    >>> 
    >>> ''.join(c if ord(c) < 128 else '_' for c in s)
    'Tannh_user'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some images that need to change, based not only on hover, but
I need to use the following regular expression to validate some Asian characters $regexp
I need to change some text values inside an after effect project file that
I've got some UITableViewCells that need to change their height depending on the length
I need that on every page change or on some elemetents (a, input[button, submit],
I've noticed that I can not use all unicode characters in my python source
I need to change a single character in a file. I'd rather not use
I need to change some custom properties values in many files. Here is an
I'm using itextsharp to generate the PDFs, but I need to change some text
I need to know when a sharer has been authorized (to change some UI)

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.