Python:在json文件中取相应的字段集

# coding:utf-8
import json
def read_file(path):
    content=''
    with open(path, 'r', encoding='utf-8') as f:
        content=f.readlines()
    print('====')
    print(type(content[0]))
    dict_content=json.loads(content[0])
    return dict_content
 
def parse_data(dict_content):
    
    data_counts=dict_content.get('data').get('list')
    
    for item in data_counts:
        temp_list=[]
        depName=item.get('depStn')
        arrName=item.get('arrStn')
        temp_list.append(depName)
        temp_list.append(arrName)
        #print(temp_list)
        #print('-'.join(temp_list))
        with open('8L.txt', 'a') as f:
            f.write('-'.join(temp_list) + '\n')

if __name__=='__main__':
    dict_content=read_file(r'C:\Users\Administrator\Desktop\8L\93.json')
    parse_data(dict_content)

 

posted @ 2019-10-22 09:08  走过不远的地方  阅读(2798)  评论(0编辑  收藏  举报