如何遍历和解析json文件的文件夹然后输出到单个fi

2024-06-16 15:29:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个json文件的文件夹,我想解析特定的键、值对。然后将这些对附加到一个字典中,然后将该字典(作为json行)输出到一个新的json文件中。我目前无法获取文件夹中的文件进行解析,更不用说将解析后的数据导入字典进行打印了。这是我的代码:

import json, os

FbDict=[]

topdir=os.getcwd() 

def main():        

    for root, dirs, files in os.walk(topdir):            
        for f in files:                        
            if f.lower().endswith((".json")):                    
                json_data = open(f, 'r+').read().decode("utf-8")
                jdata = json.loads(json_data)   
                fname=f.split(".json")[0]
                for k, v in jdata.items(): 
                    if isinstance(v, dict):                                                                
                        try:
                            dataFormat = {"created_at":v['data'][0]['created_time'],"user":v['data'][0]['from']['id'],
                                               "id":v['data'][0]['id'],"name":v['data'][0]['from']['name'],"text":v['data'][0]['message']}                                        
                                FbDict.append(json.dumps(dataFormat, separators=(',', ':')))                                                                            
                        except KeyError:
                            continue                            

if __name__ == '__main__':
    main()
    with open ('fbFile', 'w') as f:
        f.write(FbDict) 

Tags: 文件namein文件夹idjsonfordata
2条回答

以下是Python文档中缺少的部分:

http://docs.python.org/2/library/os.html#os.walk

Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

现在您只需迭代files,这是没有任何路径信息的裸文件名。添加路径信息,您应该停止获取那些“文件未找到”错误。在

感谢@rmunn&;Rob提供的帮助,以下是更新:

import json, os

FbDict=[]

def main():        

    for root, dirs, files in os.walk(os.getcwd()):            
        for f in files:                        
            if f.lower().endswith((".json")):                    
                f = os.path.join(root, f)
                with open(f, 'r') as f: json_data=f.read().decode("utf-8")
                jdata = json.loads(json_data)                       
                for k, v in jdata.items(): 
                    if isinstance(v, dict):                                                                
                        try:
                            dataFormat = {"created_at":v['data'][0]['created_time'],"user":v['data'][0]['from']['id'],
                                           "id":v['data'][0]['id'],"name":v['data'][0]['from']['name'],"text":v['data'][0]['message']}                                        
                            if dataFormat no in FbDict:
                                FbDict.append(json.dumps(dataFormat, separators=(',',':')))                          
                            else:
                                continue              
                        except KeyError:
                            continue
                f.close()

if __name__ == '__main__':
    main()
    with open ('fbFile.json', 'w') as f_out:
        for line in fbDict:
             f_out.write(line+'\n')
        f_out.close()

相关问题 更多 >