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 6105619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:59:26+00:00 2026-05-23T13:59:26+00:00

How to replace unicode values using re in Python ? I’m looking for something

  • 0

How to replace unicode values using re in Python ?
I’m looking for something like this:

line.replace('Ã','')
line.replace('¢','')
line.replace('â','')

Or is there any way which will replace all the non-ASCII characters from a file. Actually I converted PDF file to ASCII, where I’m getting some non-ASCII characters [e.g. bullets in PDF]

Please help me.

  • 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-23T13:59:27+00:00Added an answer on May 23, 2026 at 1:59 pm

    Edit after feedback in comments.

    Another solution would be to check the numeric value of each character and see if they are under 128, since ascii goes from 0 – 127. Like so:

    # coding=utf-8
    
    def removeUnicode():
        text = "hejsanäöåbadasd wodqpwdk"
        asciiText = ""
        for char in text:
            if(ord(char) < 128):
                asciiText = asciiText + char
    
        return asciiText
    
    import timeit
    start = timeit.Timer("removeUnicode()", "from __main__ import removeUnicode")
    print "Time taken: " + str(start.timeit())
    

    Here’s an altered version of jd‘s answer with benchmarks:

    # coding=utf-8
    
    def removeUnicode():
        text = u"hejsanäöåbadasd wodqpwdk"
        if(isinstance(text, str)):
            return text.decode('utf-8').encode("ascii", "ignore")
        else:
            return text.encode("ascii", "ignore")        
    
    import timeit
    start = timeit.Timer("removeUnicode()", "from __main__ import removeUnicode")
    print "Time taken: " + str(start.timeit())
    

    Output first solution using a str string as input:

    computer:~ Ancide$ python test1.py
    Time taken: 5.88719677925
    

    Output first solution using a unicode string as input:

    computer:~ Ancide$ python test1.py
    Time taken: 7.21077990532
    

    Output second solution using a str string as input:

    computer:~ Ancide$ python test1.py
    Time taken: 2.67580914497
    

    Output second solution using a unicode string as input:

    computer:~ Ancide$ python test1.py
    Time taken: 1.740680933
    

    Conclusion

    Encoding is the faster solution and encoding the string is less code; Thus the better solution.

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

Sidebar

Related Questions

How can I replace non-ascii chars from a unicode string in Python? This are
I have some text where unicode is written as text like this There areu25ba
replace newline seems to be a question asked here and there like hundred times
I would like to replace only the group in parenthesis in this expression :
I am looking to replace from a large document all high unicode characters, such
Is there a way to specify Unicode character literals in MySQL? I want to
Python 2.6 Using Python string.replace() seems not working for UTF-16-LE file. I think of
I have the following regex: foo = Regex.Replace(foo, @[^a-zA-Z0-9\s-], ); Currently, this removes Unicode
I would like to save my CSV file as unicode by using Excel. I
str_replace('Ê','',$line); Ain't working. Is there some special string that represents that?

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.