[英]For loop to iterate through a folder of images and output into a single JSON file
[英]How to walk through and parse folder of json files then output to single file
我有一個要解析的特定鍵,值對的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)
這是Python文檔中缺少的部分:
http://docs.python.org/2/library/os.html#os.walk
請注意,列表中的名稱不包含路徑成分。 要獲取dirpath中文件或目錄的完整路徑(以top開頭),請執行
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.