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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:56:27+00:00 2026-06-01T23:56:27+00:00

Is there a way to recognize if text file is UTF-8 in Python? I

  • 0

Is there a way to recognize if text file is UTF-8 in Python?

I would really like to get if the file is UTF-8 or not. I don’t need to detect other encodings.

  • 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-01T23:56:30+00:00Added an answer on June 1, 2026 at 11:56 pm

    You mentioned in a comment you only need to detect UTF-8. If you know the alternative consists of only single byte encodings, then there is a solution that often works.

    If you know it’s either UTF-8 or single byte encoding like latin-1, then try opening it first in UTF-8 and then in the other encoding. If the file contains only ASCII characters, it will end up opened in UTF-8 even if it was intended as the other encoding. If it contains any non-ASCII characters, this will almost always correctly detect the right character set between the two.

    try:
        # or codecs.open on Python <= 2.5
        # or io.open on Python > 2.5 and <= 2.7
        filedata = open(filename, encoding='UTF-8').read() 
    except:
        filedata = open(filename, encoding='other-single-byte-encoding').read() 
    

    Your best bet is to use the chardet package from PyPI, either directly or through UnicodeDamnit from BeautifulSoup:

    chardet 1.0.1

    Universal encoding detector

    Detects:

    • ASCII, UTF-8, UTF-16 (2 variants), UTF-32 (4 variants)
    • Big5, GB2312, EUC-TW, HZ-GB-2312, ISO-2022-CN (Traditional and Simplified Chinese)
    • EUC-JP, SHIFT_JIS, ISO-2022-JP (Japanese)
    • EUC-KR, ISO-2022-KR (Korean)
    • KOI8-R, MacCyrillic, IBM855, IBM866, ISO-8859-5, windows-1251 (Cyrillic)
    • ISO-8859-2, windows-1250 (Hungarian)
    • ISO-8859-5, windows-1251 (Bulgarian)
    • windows-1252 (English)
    • ISO-8859-7, windows-1253 (Greek)
    • ISO-8859-8, windows-1255 (Visual and Logical Hebrew)
    • TIS-620 (Thai)

    Requires Python 2.1 or later

    However, some files will be valid in multiple encodings, so chardet is not a panacea.

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

Sidebar

Related Questions

In emacs, is there a way to get hideshow-mode to recognize multiple regular expressions
is there way how to get name ov event from Lambda expression like with
Is there a way to recognize blank lines when you are scanning a text
Is there a way to recognize an image or something like a fingerprint or
Is there any relatively simple way to recognize Regex pattern as simple text or
I wonder if there is a way to recognize the visit from user's iphone
Is there way that I can read the file from remote server using fopen
I'm wondering if there is a way to write MIPS assembly language file (just
Is there a way to put an icon next to the text in a
Notepad++ obviously recognizes all comments as such. Is there a way to simply delete

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.