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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:13:56+00:00 2026-06-07T17:13:56+00:00

I can’t get a grip on how Python handles Unicode in files… f =

  • 0

I can’t get a grip on how Python handles Unicode in files…

f = open('test.txt', 'w')
f.write('abc')
f.close()

That gives a file of 3 bytes.

f = open('test.txt', 'w')
f.write('abcé')
f.close()

That gives a file of 5 bytes (the é takes up two bytes but how does Python knows that it must read 2 bytes there?)

f = open('test.txt', 'w')
f.write('abcそ')  # a Japanese character
f.close()

That gives a file of 6 bytes (the そ takes up three bytes but how does Python knows that it must read 3 bytes there?)

So I can understand that Unicode takes two bytes, but it is sometimes 1, or 2 or 3 bytes, I fail to see how it works.

  • 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-07T17:13:58+00:00Added an answer on June 7, 2026 at 5:13 pm

    By default, it writes the output file with an encoding of UTF-8, which is a variable-length encoding: it encodes ASCII characters (code points U+0000-U+007F) using 1 byte, code points U+0080-U+07FF (which includes Latin-1 characters such as é) using 2 bytes, code points U+0800-U+FFFF (which includes Chinese and Japanese characters such as そ) using 3 bytes, and code points U+10000-U+10FFFF using 4 bytes.

    If you want to use a different encoding, such as UTF-16, you can use str.encode to use your desired encoding:

    # Save the string as UTF-16 little-endian
    f = open('test.txt', 'w')
    f.write(u'abcそ'.encode('utf-16le')  # Output will be 8 bytes
    f.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can I be sure about the order in a Python dictionary? The function op.GetTangent(id)
Can we close all known/unknown connections to database with the code? I'm using Access
Can't seem to get the Back Button to appear in a UINavigationController flow. I
Can i get the source code for a WAMP stack installer somewhere? Any help
Can't get this working.. What am I missing here.. NodeJS, Mongoskin.. I cannot get
Can I open my custom developed form [instead of native one] when user opens
Can't get Youtube's homepage, or any other youtube.com prefixed URL to load in an
Can anyone let me know how can we change the value of kendo combobox

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.