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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:20:54+00:00 2026-05-12T10:20:54+00:00

I have a string that looks like so: 6Â 918Â 417Â 712 The clear

  • 0

I have a string that looks like so:

6 918 417 712

The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called s, we get:

s.replace('Â ', '')

That should do the trick. But of course it complains that the non-ASCII character '\xc2' in file blabla.py is not encoded.

I never quite could understand how to switch between different encodings.

Here’s the code, it really is just the same as above, but now it’s in context. The file is saved as UTF-8 in notepad and has the following header:

#!/usr/bin/python2.4
# -*- coding: utf-8 -*-

The code:

f = urllib.urlopen(url)

soup = BeautifulSoup(f)

s = soup.find('div', {'id':'main_count'})

#making a print 's' here goes well. it shows 6Â 918Â 417Â 712

s.replace('Â ','')

save_main_count(s)

It gets no further than s.replace…

  • 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-12T10:20:54+00:00Added an answer on May 12, 2026 at 10:20 am

    Python 2 uses ascii as the default encoding for source files, which means you must specify another encoding at the top of the file to use non-ascii unicode characters in literals. Python 3 uses utf-8 as the default encoding for source files, so this is less of an issue.

    See:
    http://docs.python.org/tutorial/interpreter.html#source-code-encoding

    To enable utf-8 source encoding, this would go in one of the top two lines:

    # -*- coding: utf-8 -*-
    

    The above is in the docs, but this also works:

    # coding: utf-8
    

    Additional considerations:

    • The source file must be saved using the correct encoding in your text editor as well.

    • In Python 2, the unicode literal must have a u before it, as in s.replace(u"Â ", u"") But in Python 3, just use quotes. In Python 2, you can from __future__ import unicode_literals to obtain the Python 3 behavior, but be aware this affects the entire current module.

    • s.replace(u"Â ", u"") will also fail if s is not a unicode string.

    • string.replace returns a new string and does not edit in place, so make sure you’re using the return value as well

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

Sidebar

Related Questions

Say, we have a query string that looks like this: param1:'test1' && param2:'test2' I
I have a String that looks like this String read = 1130:5813|1293:5803|1300:5755|1187:5731| As you
I have a string that looks like this: blah blah blah Team ID:</div>xxxxxxx blah
If I have a string that looks like this: This is a string with
I have an XML string that looks like this: <Attributes> <ProductAttribute id=1> <ProductAttributeValue> <Value>a</Value>
I have an action method that looks like this: public ActionResult DoSomething(string par, IEnumerable<string>
So I have a JSONObject (or String..) that looks like this: {locations:[{GeocodeResponse:{result:{formatted_address:Tchibanga (TCH), Gabon,address_component:[{long_name:Tchibanga,type:
I have a string that looks like A=1.23;B=2.345;C=3.567 I am only interested in C=3.567
I have a string of text that looks like the following: Thank you for
I have three floats that I want to output as a 2 decimal places

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.