我有一个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)
答案 0 :(得分:1)
以下是您缺少的Python文档中的部分:
http://docs.python.org/2/library/os.html#os.walk
请注意,列表中的名称不包含路径组件。要获取 dirpath 中的文件或目录的完整路径(以 top 开头),请执行
os.path.join(dirpath, name)
。
现在你只是在files
上进行迭代,这是没有任何路径信息的裸文件名。添加路径信息,您应该停止获取“找不到文件”错误。
答案 1 :(得分:1)
谢谢@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()