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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:49:29+00:00 2026-06-05T12:49:29+00:00

My background is in Perl, but I’m giving Python plus BeautifulSoup a try for

  • 0

My background is in Perl, but I’m giving Python plus BeautifulSoup a try for a new project.

In this example, I’m trying to extract and present the link targets and link text contained in a single page. Here’s the source:

table_row = u'<tr><td>{}</td><td>{}</td></tr>'.encode('utf-8')
link_text = unicode(link.get_text()).encode('utf-8')
link_target = link['href'].encode('utf-8')
line_out = unicode(table_row.format(link_text, link_target))

All those explicit calls to .encode(‘utf-8’) are my attempt to make this work, but they don’t seem to help — it’s likely that I’m completely misunderstanding something about how Python 2.7 handles Unicode strings.

Anyway. This works fine up until it encounters U+2013 in a URL (yes, really). At that point it bombs out with:

Traceback (most recent call last):
File "./test2.py", line 30, in <module>
  line_out = unicode(table_row.encode('utf-8').format(link_text, link_target.encode('utf-8')))
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 79: ordinal not in range(128)

Presumably .format(), even applied to a Unicode string, is playing silly-buggers and trying to do a .decode() operation. And as ASCII is the default, it’s using that, and of course it can’t map U+2013 to an ASCII character, and thus…

The options seem to be to remove it or convert it to something else, but really what I want is to simply preserve it. Ultimately (this is just a little test case) I need to be able to present working clickable links.

The BS3 documentation suggests changing the default encoding from ASCII to UTF-8 but reading comments on similar questions that looks to be a really bad idea as it’ll muck up dictionaries.

Short of using Python 3.2 instead (which means no Django, which we’re considering for part of this project) is there some way to make this work cleanly?

  • 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-05T12:49:30+00:00Added an answer on June 5, 2026 at 12:49 pm

    First, note that your two code samples disagree on the text of the problematic line:

    line_out = unicode(table_row.encode('utf-8').format(link_text, link_target.encode('utf-8')))
    

    vs

    line_out = unicode(table_row.format(link_text, link_target))
    

    The first is the one from the traceback, so it’s the one to look at. Assuming the rest of your first code sample is accurate, table_row is a byte-string, because you took a unicode string and encoded it. Byte strings can’t be encoded, so Python 2 implicitly converts table_row from byte-string to unicode by decoding it as ascii. Hence the error message, "UnicodeDecodeError from ascii".

    You need to decide what strings will be byte strings and which will be unicode strings, and be disciplined about it. I recommend keeping all text as Unicode strings as much as possible.

    Here’s a presentation I gave at PyCon that explains it all: Pragmatic Unicode, or, How Do I Stop The Pain?

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

Sidebar

Related Questions

I'm new to this as I come from C++ and Perl background. Trying to
I am very new to linux and perl and I am trying to use
I'm pretty new to Perl but have been programming in java for several months
I'm trying to figure out how to start a background process in a perl
With Java and Perl background, I have narrowed down the web framework choices to
A little background: I have a perl script which is performing a number of
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Okay, coming from a perl background I am used to Arrays holding refs to
I have two processes: a server that should be run in background, but starts

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.