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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:10:08+00:00 2026-06-12T15:10:08+00:00

When I use simple I/o calls to read a particular file on my system,

  • 0

When I use simple I/o calls to read a particular file on my system, such as:

f = open('file.ini')
for line in f.readlines():
    print line

I’m getting output such as this:

 H E L L O !  W H Y  A R E  T H E R E  S O  M A N Y  S P A C E S ?

I presume it’s Unicode but I can’t quite figure out how to read it as Unicode / convert it to ascii. Suggestions?

  • 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-12T15:10:10+00:00Added an answer on June 12, 2026 at 3:10 pm

    V e r y   r e g u l a r   s p a c e s are usually an indicator that your data is encoded in UTF16 — Usually what you see is that every second byte is a 0 byte. You can confirm this by printing out the actual binary data that you are reading:

    f = open('file.ini')
    line in f.readline():
    print map(ord, line)
    

    If you see output like this:

    [..., 68, 0, 65, 0, 76, 0, 76, 0, 79, ...]
    

    Then that’s almost certainly the case.

    The trick, then, is to figure out whether it’s the even bytes that are 0s, or the odd bytes. There are two UTF-16 encodings: Big-endian and little-endian, named for the significance of the byte that comes first. If your 0s come before the character that they are associated with, then the file is big-endian, and you can open it like this (Python 3.x):

    f = open('file.ini', encoding='utf16be')
    

    In Python 2.x, import the codecs module to do this:

    import codecs
    f = codecs.open('file.ini', encoding='utf16be')
    

    If the 0s come after, then substitude ‘utf16le’.

    (You need to make sure that you decode the file as you’re reading it, or read the entire contents into memory before decoding. You definitly do not want to split lines apart before you decode)

    If you’re lucky, then the file was written with a Byte Order Mark at the beginning this character is U+FEFF– if the first two bytes are [254, 255], then the encoding is big-endian, and if [255, 254], then it is little-endian.

    If none of those apply, then you might not be looking at UTF-16 data, and you’ll have to do some more research to figure out what encoding you’re looking at.

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

Sidebar

Related Questions

i am creating simple call filter application which restrict unwanted calls. i use following
This example demonstrates using Scanner to read a file line by line (it does
I want to create a simple AJAX call, based on How to use Servlets
How to use simple TCP/IP sockets connections over/with Net.TCP WCF binding? Idea is –
I'm trying to use Simple Savant within my application, to use SimpleDB I currently
We often use simple enumerations to represent a state on our entities. The problem
Is there a way to use simple sql queries on ASP MVC without using
I've always thought it was great that I could use simple iconic unicode characters
I've used ITK with c++ some time ago and I'm trying to use simple
This seems like a fairly straightforward question, but I couldn't find this particular use-case

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.