So I have this Json file which I’m using to extract and output the following inf
ID: 3908506 • Letter: S
Question
So I have this Json file which I’m using to extract and output the following info in a python program:
Output:
DEN-ORL 500
ORL-DEN 500
LAX-DEN 150
DEN-LAX 150
And so on..
What should be the correct code in python3 do a searchD for example DEN? and it will display only LAX-DEN as destination
{
"cities": [{
"name": ["DEN","ORL"],
"price": 450
}, {
"name": ["ORL","LAG"],
"price": 500
}, {
"name": ["LAX","DEN"],
"price": 150
}, {
"name": ["BOS","AUS"],
"price": 500
}, {
"name": ["MIA","LAS"],
"price": 400
}, {
"name": ["MCI","CLT"],
"price": 340
}]
}
Explanation / Answer
import json def main(): filename = input('Enter file name: ') code = input('Enter city code: ') cities = json.load(open(filename, 'r'))['cities'] for city in cities: if city['name'][1] == code: print('-'.join(city['name'])) main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.