Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Python)字符串值不正确(CP1521到UTF8)_Python_Json_Cp1251 - Fatal编程技术网

(Python)字符串值不正确(CP1521到UTF8)

(Python)字符串值不正确(CP1521到UTF8),python,json,cp1251,Python,Json,Cp1251,包含西里尔字母符号的.json文件有问题。如何将CP1251转换为UTF-8? (temp_data.decode('utf-8')无效,例如确保ascii=Falsein.dumps) 您可以传递,如果确保\u ascii为true(默认值),则输出中的所有非ascii字符都将使用\uxxx序列转义,并且结果是仅由ascii字符组成的str实例。如果确保\u ascii为false,则结果可能是Unicode实例。如果输入包含Unicode字符串或使用了编码参数,则通常会发生这种情况 将代码更

包含西里尔字母符号的.json文件有问题。如何将CP1251转换为UTF-8? (temp_data.decode('utf-8')无效,例如确保ascii=Falsein.dumps)

您可以传递,如果
确保\u ascii
为true(默认值),则输出中的所有非ascii字符都将使用\uxxx序列转义,并且结果是仅由ascii字符组成的str实例。如果
确保\u ascii
为false,则结果可能是Unicode实例。如果输入包含Unicode字符串或使用了编码参数,则通常会发生这种情况

将代码更改为:

out_json = json.dumps(d, sort_keys=True, indent=4, separators = (',', ': '), ensure_ascii=False)
还有一个完整的代码:

import json

def load_data(filepath):   
    with open(filepath, 'r') as f:
        temp_data = json.load(f)
    return temp_data 


def pretty_print_json(d):
    out_json = json.dumps(d, sort_keys=True, indent=4, separators = (',', ': '), ensure_ascii=False)
    print(out_json)

if __name__ == '__main__':
    print("Enter the path to .json file: ") 
    in_path = raw_input()
    print("There are pretty printed json format: ")
    pretty_print_json(load_data(in_path))
我用这个JSON文件测试了代码


您可以在中看到结果。

这是可行的。提供数据文件的示例,如果数据不符合以下条件,请指定编码:

#coding:utf8
import json

datafile_encoding = 'cp1251'  # Any encoding that supports Cyrillic works.

# Create a test file with Cyrillic symbols.
with open('test.json','w',encoding=datafile_encoding) as f:
    D = {'key':'АБВГДЕЖЗИЙКЛМНОПРСТ', 'key2':'АБВГДЕЖЗИЙКЛМНОПРСТ'}
    json.dump(D,f,ensure_ascii=False)

# specify the encoding of the data file
def load_data(filepath):   
    with open(filepath, 'r', encoding=datafile_encoding) as f:
        temp_data = json.load(f)
    return temp_data 

# Use ensure_ascii=False
def pretty_print_json(d):
    out_json = json.dumps(d, sort_keys=True, ensure_ascii=False, indent=4, separators = (',', ': '))
    print(out_json)

if __name__ == '__main__':
    in_path = 'test.json'
    pretty_print_json(load_data(in_path))

你有什么问题?显示一个示例数据文件、所需输出和实际输出。数据文件包含俄文单词,如“БАББББССА”和“БААЛАЙСА”,但结果是这些单词被视为“/u0439/u0440”等。数据文件的编码是什么?用细节更新你的问题。添加一个复制问题的小样本数据。@Double\u Mind:
'\u0439\u0440'==“lц”
,这样它就可以正常工作了。你能把文件的内容贴出来吗<代码>打印(repr(打开(您的_文件名'rb').read())UnicodeEncodeError:'charmap'编解码器无法对1328位置的字符'\xab'进行编码:字符映射到哪个Python?我用Python3测试了一下,成功了!Python2:)你能给我们一个json的例子吗?正如我所说,Python3.4包含西里尔文。我的系统可能有问题,我不知道know@Double_Mind我是这样品尝的:
#coding:utf8
import json

datafile_encoding = 'cp1251'  # Any encoding that supports Cyrillic works.

# Create a test file with Cyrillic symbols.
with open('test.json','w',encoding=datafile_encoding) as f:
    D = {'key':'АБВГДЕЖЗИЙКЛМНОПРСТ', 'key2':'АБВГДЕЖЗИЙКЛМНОПРСТ'}
    json.dump(D,f,ensure_ascii=False)

# specify the encoding of the data file
def load_data(filepath):   
    with open(filepath, 'r', encoding=datafile_encoding) as f:
        temp_data = json.load(f)
    return temp_data 

# Use ensure_ascii=False
def pretty_print_json(d):
    out_json = json.dumps(d, sort_keys=True, ensure_ascii=False, indent=4, separators = (',', ': '))
    print(out_json)

if __name__ == '__main__':
    in_path = 'test.json'
    pretty_print_json(load_data(in_path))
{
    "key": "АБВГДЕЖЗИЙКЛМНОПРСТ",
    "key2": "АБВГДЕЖЗИЙКЛМНОПРСТ"
}