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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:32:28+00:00 2026-05-11T13:32:28+00:00

This is in python 2.4. Here is my situation. I pull a string from

  • 0

This is in python 2.4. Here is my situation. I pull a string from a database, and it contains an umlauted ‘o’ (\xf6). At this point if I run type(value) it returns str. I then attempt to run .decode(‘utf-8’), and I get an error (‘utf8’ codec can’t decode bytes in position 1-4).

Really my goal here is just to successfully make type(value) return unicode. I found an earlier question that had some useful information, but the example from the picked answer doesn’t seem to run for me. Is there something I am doing wrong here?

Here is some code to reproduce:

Name = 'w\xc3\xb6rner'.decode('utf-8') file.write('Name: %s - %s\n' %(Name, type(Name))) 

I never actually get to the write statement, because it fails on the first statement.

Thank you for your help.

Edit:

I verified that the DB’s charset is utf8. So in my code to reproduce I changed ‘\xf6’ to ‘\xc3\xb6’, and the failure still occurs. Is there a difference between ‘utf-8’ and ‘utf8’?

The tip on using codecs to write to a file is handy (I’ll definitely use it), but in this scenario I am only writing to a log file for debugging purposes.

  • 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. 2026-05-11T13:32:28+00:00Added an answer on May 11, 2026 at 1:32 pm

    So in my code to reproduce I changed ‘\xf6’ to ‘\xc3\xb6’, and the failure still occurs

    Not in the first line it doesn’t:

    >>> 'w\xc3\xb6rner'.decode('utf-8') u'w\xf6rner' 

    The second line will error out though:

    >>> file.write('Name: %s - %s\n' %(Name, type(Name))) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 7: ordinal not in range(128) 

    Which is entirely what you’d expect, trying to write non-ASCII Unicode characters to a byte stream. If you use Jiri’s suggestion of a codecs-wrapped stream you can write Unicode directly, otherwise you will have to re-encode the Unicode string into bytes manually.

    Better, for logging purposes, would be simply to spit out a repr() of the variable. Then you don’t have to worry about Unicode characters being in there, or newlines or other unwanted characters:

    name= 'w\xc3\xb6rner'.decode('utf-8') file.write('Name: %r\n' % name)  Name: u'w\xf6rner' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Figured it out: map = new GMap2(document.getElementById("map"), { backgroundColor: "#000000"… May 11, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer My best answer is for you to learn both. Consider… May 11, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer With substr(): substr($string, 0, -7); I suggest you read the… May 11, 2026 at 4:57 pm

Related Questions

I am writing a Python utility that needs to parse a large, regularly-updated CSV
I'm having some trouble updating a row in a MySQL database. Here is the
I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this
I am trying to numerically integrate an arbitrary (known when I code) function in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.