读取 json 包含德语字符的文件 python 2 (ironpython)

read json file with german characters python 2 (ironpython)

我正在使用 Ironpython,所以 python 2 并读取带有德语字符的 .json 文件我正在使用编码='utf-8' 但我得到以下错误:open() got an unexpected keyboard argument 'encoding'.

这里有一个代码示例:

   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

python 2.x 不支持 encoding 参数。您必须导入 io 模块以指定编码

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

Python2也不支持直接打开功能

如此处所示:

你可以这样使用:

import codecs
    f = codecs.open(fileName, 'r', errors = 'ignore')

open 中的 encoding 参数已添加到 Python 3. 如果要在 python 2.x 中使用编码,请使用以下内容:

import io.open

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