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

  • Home
  • SEARCH
  • 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 9315959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:29:45+00:00 2026-06-19T02:29:45+00:00

I was wondering whether the python library has a function that returns a file’s

  • 0

I was wondering whether the python library has a function that returns a file’s character encoding by looking for the presence of a BOM.

I’ve already implemented something, but I’m just afraid I might be reinventing the wheel

Update: (based on John Machin’s correction):

import codecs

def _get_encoding_from_bom(fd):
    first_bytes = fd.read(4)
    fd.seek(0)
    bom_to_encoding = (
        (codecs.BOM_UTF32_LE, 'utf-32'),
        (codecs.BOM_UTF32_BE, 'utf-32'),
        (codecs.BOM_UTF8, 'utf-8-sig'),
        (codecs.BOM_UTF16_LE, 'utf-16'),
        (codecs.BOM_UTF16_BE, 'utf-16'),
        )
    for bom, encoding in bom_to_encoding:
        if first_bytes.startswith(bom):
             return encoding
    return None
  • 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-19T02:29:46+00:00Added an answer on June 19, 2026 at 2:29 am

    Your code has a subtle bug that you may never be bitten by, but it’s best that you avoid it.

    You are iterating over a dictionary’s keys. The order of iteration is NOT guaranteed by Python. In this case order does matter.

    codecs.BOM_UTF32_LE is '\xff\xfe\x00\x00'
    codecs.BOM_UTF16_LE is '\xff\xfe'
    

    If your file is encoded in UTF-32LE but UTF-16LE just happens to be tested first, you will incorrectly state that the file is encoded in UTF-16LE.

    To avoid this, you can iterate over a tuple that is ordered by BOM-length descending. See sample code in my answer to this question.

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

Sidebar

Related Questions

I was wondering whether anyone knows definitively if LINQ to SQL has the capability
I was wondering whether there're recommendable sites, that host collections of good and useful
I am just wondering whether that is a correct syntax in JQuery : var
I am wondering whether there is a way to upload a zip file to
I am wondering whether there's a lightweight way to distribute python tasks among different
I am wondering whether python's shutil.move is atomic on linux ? Is the behavior
I was wondering whether there is a trick to (easily) incorporate some python code
Just wondering whether anyone knows how to get blogger labels into alt tags in
I wondering whether use a webView to create a complex text area done with
I'm wondering whether or not there are any restrictions during HBase minor/major compactions .

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.