2

I am working with Ironpython, so python 2 and to read the .json file with German characters I am using encoding='utf-8' but I get the following error: open() got an unexpected keyboard argument 'encoding'.

Here an example of the code:

   def get_data(self):
        #Open and read data from json file into dict
        with open(self.file, encoding='utf-8') as j_file:
            data_dict = json.load(j_file)
            return data_dict
ti7
  • 16,375
  • 6
  • 40
  • 68
soynachet
  • 23
  • 2
  • Does this answer your question? [Backporting Python 3 open(encoding="utf-8") to Python 2](https://stackoverflow.com/questions/10971033/backporting-python-3-openencoding-utf-8-to-python-2) – ti7 Jan 29 '21 at 18:49

3 Answers3

2

python 2.x doesn't support the encoding parameter. You must import the io module to specify the encoding

open Function - pythontips

import io

   def get_data(self):
        #Open and read data from json file into dict
        with io.open(self.file, encoding='utf-8') as j_file:
            data_dict = json.load(j_file)
            return data_dict
el_oso
  • 1,021
  • 6
  • 10
1

Python 2 also does not support the open function directly.

As seen here: https://stackoverflow.com/a/30700186/12711820

You can use something like this:

import codecs
    f = codecs.open(fileName, 'r', errors = 'ignore')
Shyrtle
  • 647
  • 5
  • 20
1

The encoding argument in open was added in Python 3. If you want to use encoding in python 2.x, use the following:

import io.open

f = io.open('file.json', encoding='utf-8')
# Do stuff with f
f.close
CATboardBETA
  • 418
  • 6
  • 29