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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:19:03+00:00 2026-06-11T09:19:03+00:00

I have a simple program that loads a .json file which contains a funny

  • 0

I have a simple program that loads a .json file which contains a funny character. The program (see below) runs fine in Terminal but gets this error in IntelliJ:

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position
2: ordinal not in range(128)

The crucial code is:

with open(jsonFileName) as f:
    jsonData = json.load(f)

if I replace the open with:

with open(jsonFileName, encoding='utf-8') as f:

Then it works in both IntelliJ and Terminal. I’m still new to Python and the IntelliJ plugin, and I don’t understand why they’re different. I thought sys.path might be different, but the output makes me think that’s not the cause. Could someone please explain? Thanks!

Versions:

  • OS: Mac OS X 10.7.4 (also tested on 10.6.8)
  • Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) /Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2
  • IntelliJ: 11.1.3 Ultimate

Files (2):

1. unicode-error-demo.py

#!/usr/bin/python

import json
from pprint import pprint as pp
import sys

def main():
    if len(sys.argv) is not 2:
        print(sys.argv[0], "takes one arg: a .json file")
        return

    jsonFileName = sys.argv[1]
    print("sys.path:")
    pp(sys.path)
    print("processing", jsonFileName)

#    with open(jsonFileName) as f:           # OK in Terminal, but BUG in IntelliJ: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 2: ordinal not in range(128)
    with open(jsonFileName, encoding='utf-8') as f:     # OK in both
        jsonData = json.load(f)
        pp(jsonData)


if __name__ == "__main__":
    main()

2. encode-temp.json

["™"]
  • 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-11T09:19:04+00:00Added an answer on June 11, 2026 at 9:19 am

    The JSON .load() function expects Unicode data, not raw bytes. Python automatically tries to decode the byte string to a Unicode string for you using a default codec (in your case ASCII), and fails. By opening the file with the UTF-8 codec, Python makes an explicit conversion for you. See the open() function, which states:

    In text mode, if encoding is not specified the encoding used is platform dependent.

    The encoding that would be used is determined as follows:

    • Try os.device_encoding() to see if there is a terminal encoding.
    • Use locale.getpreferredencoding() function, which depends on the environment you run your code in. The do_setlocale of that function is set to False.
    • Use 'ASCII' as a default if both methods have returned None.

    This is all done in C, but it’s python equivalent would be:

    if encoding is None:
        encoding = os.device_encoding()
    if encoding is None:
        encoding = locale.getpreferredencoding(False)
    if encoding is None:
        encoding = 'ASCII'
    

    So when you run your program in a terminal, os.deviceencoding() returns 'UTF-8', but when running under IntelliJ there is no terminal, and if no locale is set either, python uses 'ASCII'.

    The Python Unicode HOWTO tells you all about the difference between unicode strings and bytestrings, as well as encodings. Another essential article on the subject is Joel Spolsky’s Absolute Minimum Unicode knowledge article.

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

Sidebar

Related Questions

I have a very simple program, that loads an wavefront obj file, rotate and
I have a simple Delphi program that I'm working on, in which I am
Say I have simple program that emulates a board game with a number of
I have a simple program that checks webpages for strings, example: Private Sub Button1_Click(ByVal
I have this simple program that computes salaries for four different worker types. It's
I have a simple program that creates a thread, loops twenty times and then
I have a simple program that reads data from a PNG into a 2D
I have a simple python program that I'd like to daemonize. Since the point
I have a simple forms program that I have been fighting with for a
Well, i have written a simple python program that parses HTML with HTMLParser. Here

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.