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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:52:23+00:00 2026-05-31T16:52:23+00:00

I am reading a file in utf-8 into unicode and I do not get

  • 0

I am reading a file in utf-8 into unicode and I do not get any errors.

try:
        f = codecs.open(fil_name, "r","utf-8")
        f_str = f.read()

That is, the string f_str is in “unicode”
Later in the program I have to send the (u) string in f_str to a socket. I am trying to convert the string back to “utf-8”.

usock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
usock.connect(("xxx server", 123))
usock.send("TEXT %s\nENDQ\n" % f_str.replace("\n", " ").encode("utf-8"))

here I am getting an error message:

usock.send("TEXT %s\nENDQ\n" % text.replace("\n", " ").encode("utf-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 41: ordinal not in range(128)

In my text, I have characters that cannot be coded with pure ASCII (äö..) but it is not a problem with utf-8 or latin-1.
Why I am getting this error? I am not using ASCII, I am using unicode/utf-8???

  • 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-31T16:52:24+00:00Added an answer on May 31, 2026 at 4:52 pm

    Your string literal is a byte string. When you try to inperpolate into it Python will implicitly try to convert to byte string using the default encoding (ascii).

    There are a couple of ways to fix this. One is just use Python 3. 😉

    If you are using Python 2 then put the following at the top of the source file:

    from __future__ import unicode_literals
    

    Then your literal will be unicode also.

    You could also prefix the string with a ‘u’.

    Another problem with that line is precedence. The ‘%s’ format operation is what is trying to convert your unicode into a string implicitly, using the ascii codec, after the right side is complete.

    So, try this:

    (u"TEXT %s\nENDQ\n" % f_str.replace(u"\n", u" ")).encode("utf-8")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I have a text file that is encoded in UTF-8. I'm reading it in
Are there any good configuration file reading libraries for C\C++ that can be used
I have a large file(60mb) and I am reading the file into a string
I am taking an XML file and reading it into various strings, before writing
My app downloads a file in UTF-8 format, which is too large to read
In the comments to my answer about reading an entire file into memory using
Hi there I have been trying to read an xml file with UTF strings,
i am trying to read jpg file and convert into byte array this is
I have a file saved as UTF-8, and i'm reading it like this: ReadFile(hFile,

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.