Reputation: 557
I have a Json file which I'm parsing data and it's generated output is in output.txt. At this moment, after the output.txt is generated i'm reading output.txt line by line. Splitting each line and then deleting first two column.
("\t".join(line.split()[2:]) + "\n")
How can I get the same result from for loop shared below? Expected output 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
expected output.txt
android/hello/src/hello.cpp
android/hello/src/hell1o.cpp
android/helloworld/src/hello1.cpp
Upvotes: 0
Views: 852
Reputation: 107124
You can iterate through the sub-lists like this:
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:])
This outputs:
android/hello/src/hello.cpp
android/hello/src/hell1o.cpp
android/helloworld/src/hello1.cpp
Upvotes: 2
Reputation: 1007
This will do the trick
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)
Note the use of with
to open the file which will also close it afterwards.
I used regex to make this more robust, this will get anything of the from numbers numbers numbers/stuff_to_match
Upvotes: 2