简体   繁体   English

使用python修改Json文件的格式

[英]Using python to modify format of Json file

I have JSON file that is formatted like this: (multi-line for clarity) 我有这样格式化的JSON文件:(为清晰起见,多行显示)

(line 0001)....... (0001行).......

{
    "_id": "iD_0001",
    "skills": [{
        "name": "Project Management"
    }, {
        "name": "Business Development"
    }]
}

.... (line 9999) ....(9999行)

{
    "_id":"iD_9999",
    "skills": [{
        "name": "Negotiation"
    }, {
        "name": "Banking"
    }]
}

I'd like to run a program on it, however, the program cannot read it under the aforementioned format. 我想在上面运行一个程序,但是该程序无法以上述格式读取它。 Thus I'd like to modify its format to: 因此,我想将其格式修改为:

[{
    "_id": "iD_0001",
    "skills": [{
        "name": "Project Management"
    }, {
        "name": "Business Development"
    }]
},{
    "_id":"iD_9999",
    "skills": [{
        "name": "Negotiation"
    }, {
        "name": "Banking"
    }]
}]

Essentially, putting all entries in a single array. 本质上,将所有条目放在单个数组中。 Is there a way to implement that using Python or demjson? 有没有一种方法可以使用Python或demjson来实现?

ALTERNATIVE: I made a program that fetches the skills in these json files and sends them to a text file (Test.txt), however it only works for the second format, not the first. 替代:我编写了一个程序来获取这些json文件中的技能并将其发送到文本文件(Test.txt),但是该程序仅适用于第二种格式,不适用于第一种格式。 Can you suggest a modification to make it work for the first format (above)? 您能否建议修改使其适用于第一种格式(上述)? This is my program: 这是我的程序:

import json
from pprint import pprint
with open('Sample.json') as data_file:    
    data = json.load(data_file)

    with  open('Test.txt', 'w') as f:
        for x in data:
            for y in x["skills"]: 
                    f.write(y["name"])
        f.close()

SOLUTION

Thank you to Antti Haapala for noticing the catenation of Json objects under the first format, as well as to Walter Witzel and Josh J for suggesting alternative answers. 感谢Antti Haapala注意第一种格式下的Json对象的分类,以及Walter Witzel和Josh J提出的替代答案。 Since the first format is a catenation of individual objects, the program functions well if we load the first Json file Line-by-Line instead of as a whole. 由于第一格式是个体对象的连环,程序运行良好,如果我们加载第一个JSON文件行由行 ,而不是作为一个整体。 I have done that with: 我已经做到了:

data = []
with open('Sample1-candidats.json') as data_file:    
for line in data_file:
    data.append(json.loads(line))

    with  open('Test.txt', 'w') as f:
        for x in data:
            for y in x["skills"]: 
                    f.write(y["name"])
        f.close()

Here it goes. 来了 This assumes that your file is just a bunch of individual json objects concatenated and you need to transform in a list of json objects. 这假设您的文件只是一堆串联的单个json对象,并且您需要在json对象列表中进行转换。

import json
from pprint import pprint

with open('sample.json') as data_file:    
    strData = '[' + ''.join(data_file.readlines()).replace('}\n{','},{') + ']'
    data = eval(strData)

with  open('Test.txt', 'w') as f:
    for x in data:
        for y in x["skills"]: 
            f.write(y["name"])

Here are the steps you can take to accomplish your problem. 这是您可以采取的解决问题的步骤。 Since it kinda sounds like a homework assignment, I will give you the logic and pointers but not the code. 由于这听起来像是一项家庭作业,因此我将为您提供逻辑和指针,而不是代码。

  1. Open the file for reading 打开文件进行阅读
  2. Read file into string variable (if small enough for memory limits) 将文件读入字符串变量(如果足够小以限制内存)
  3. Create empty list for output 创建用于输出的空list
  4. Split string on ..... .....上分割字符串
  5. json.loads each piece of resulting list json.loads每个结果列表
  6. Append each result to your empty output list 将每个结果附加到空的输出列表中
  7. Have a cup of coffee to celebrate 喝杯咖啡庆祝

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM