Python分割键值

时间:2019-01-29 20:51:54

标签: python json

我有一个Json文件,正在解析数据,它的生成输出在output.txt中。此时,在生成output.txt之后,我正在逐行读取output.txt。拆分每行,然后删除前两列。

("\t".join(line.split()[2:]) + "\n") 

如何从下面共享的for循环中获得相同的结果? 预期输出project_name + Files_name。

script.py

import json
x = json.load(open('data.json'))
for sub_dict in x['changed']:
    print('project_name', sub_dict['project_name'])

    for entry in sub_dict['added_commits']:
        print (entry['File_Names'])
  

data.json

{
    "changed": [
        {
            "prev_revision": "a09936ea19ddc9f69ed00a7929ea81234af82b95", 
            "added_commits": [
                {
                    "File_Names": [
                        "115\t0\t1/src/hello.cpp",
                        "116\t0\t1/src/hell1o.cpp"
                    ], 
                }
            ], 
            "project_name": "android/hello"
        }, 
       {
            "prev_revision": "a09936ea19ddc9f69ed00a7929ea81234af82b95", 
            "added_commits": [
                {
                    "File_Names": [
                        "41\t1\t1/src/hello1.cpp" 
                    ], 
                }
            ], 
            "project_name": "android/helloworld"
        }

    ]
}
  

output.txt

115 0   1/src/hello.cpp
116 0   1/src/hell1o.cpp
41  1   1/src/hello1.cpp
  

预期的output.txt

android/hello/src/hello.cpp
android/hello/src/hell1o.cpp
android/helloworld/src/hello1.cpp

2 个答案:

答案 0 :(得分:2)

这可以解决问题

import json
import re
with open('data.json') as f:
    x = json.load(f)

for sub_dict in x['changed']:
    proj = sub_dict['project_name']

    for entry in sub_dict['added_commits']:
        for name in entry['File_Names']:
            n = re.findall(r'(?:\s*\d+\s*\d+\s*\d+)(\/.*)', name)[0]
            print( proj + n)

请注意使用with打开文件,此文件随后也会关闭。

我使用了正则表达式来使其更强大,这将从numbers numbers numbers/stuff_to_match中获得任何好处

答案 1 :(得分:2)

您可以像这样遍历子列表:

for d in x['changed']:
    for c in d['added_commits']:
        for f in c['File_Names']:
            print(d['project_name'] + f.split('\t')[2][1:])

这将输出:

android/hello/src/hello.cpp
android/hello/src/hell1o.cpp
android/helloworld/src/hello1.cpp