Python:merging json将“”改为“\u00c3\u00b7”

goucqfw6  于 5个月前  发布在  Python
关注(0)|答案(1)|浏览(62)

我尝试编码utf-8和cp 1252,但有类似的输出
出于说明目的,file1.json = file2.json

[{"subject":"Math","operation":[{"sign":"÷"},{"sign":"+"}]}]

字符串
Python代码

json_files = ["file1.json", "file2.json"]    
def merge_JsonFiles(json_files):
  merge_jsonFile = 'merged.json'
  result = list()
  for f1 in json_files:
    # with open(f1, 'r', encoding='cp1252') as infile:
    # with open(f1, 'r', encoding='utf-8') as infile:
    with open(f1, 'r') as infile:
      result.extend(json.load(infile))

  with open(merge_jsonFile, 'w') as output_file:
    json.dump(result, output_file)
    
merge_JsonFiles(json_files)

ssm49v7z

ssm49v7z1#

像这样试试

import json

json_files = ["file1.json", "file2.json"]    
def merge_JsonFiles(json_files):
    merge_jsonFile = 'merged.json'
    result = list()
    for f1 in json_files:
        with open(f1, 'r') as infile:
            result.extend(json.load(infile))

    with open(merge_jsonFile, 'w', encoding='utf-8') as output_file:
        json.dump(result, output_file, ensure_ascii=False)

merge_JsonFiles(json_files)

字符串

相关问题