{"id":50772,"date":"2024-12-03T09:19:02","date_gmt":"2024-12-03T01:19:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50772\/"},"modified":"2024-12-03T09:19:02","modified_gmt":"2024-12-03T01:19:02","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e4%bb%a3%e7%a0%81%e4%bf%ae%e6%94%b9-json-%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84%e5%ad%97%e6%ae%b5%e5%b9%b6%e5%a4%8d%e5%88%b6%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50772\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17327032546746f416737a2.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4fee\u6539 json \u548c\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84<\/strong><\/p>\n<p>\u4ee5\u4e0b python \u4ee3\u7801\u53ef\u7528\u4e8e\u4fee\u6539 json \u6587\u4ef6\u4e2d\u7684\u90e8\u5206\u5b57\u6bb5\uff0c\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684 json \u6587\u4ef6\u3001jpg \u6587\u4ef6\u548c avi \u6587\u4ef6\u5230\u65b0\u8def\u5f84\u3002<\/p>\n<pre>import os\nimport json\nimport shutil\n\n# \u83b7\u53d6 json \u6587\u4ef6\u7684\u8def\u5f84\ndef get_json_data(old_json_path):\n    json_file_paths = []\n    for root, dirs, files in os.walk(old_json_path):\n        for file_name in files:\n            if file_name.endswith('.json'):\n                old_json_file_path = os.path.join(root, file_name)\n                json_file_paths.append(old_json_file_path)\n    return json_file_paths\n\n# \u83b7\u53d6\u6587\u4ef6\u5939\u7684\u8def\u5f84\ndef get_dir_name(dir_name):\n    dir_names = []\n    for root, dirs, files in os.walk(dir_name):\n        for dir in dirs:\n            dirpath = os.path.join(root, dir)\n            dir_names.append(dirpath)\n    return dir_names\n\n# \u8bfb\u53d6 json \u6587\u4ef6\u5e76\u66f4\u65b0\u5b57\u6bb5\ndef read_json(filepath):\n    with open(filepath, 'r', encoding='utf-8') as f:\n        json_data = f.read()\n        if json_data:\n            json_data_dict = eval(json_data)\n            json_data_dict.update({\"filename\": filepath.split('\\\\')[-2].replace('.dav', '.avi')})\n    return json_data_dict\n\n# \u5199\u5165 json \u6587\u4ef6\ndef write_json(filepath, newdata):\n    with open(filepath, 'w', encoding='utf-8') as r:\n        json.dump(newdata, r, indent=4, ensure_ascii=false)\n\n# \u590d\u5236 json \u6587\u4ef6\u548c\u5176\u4ed6\u6587\u4ef6\u5230\u65b0\u8def\u5f84\ndef copy_files(old_path, new_path):\n    dir_names = get_dir_name(old_path)\n    for dir in dir_names:\n        json_file_list = get_json_data(dir)\n        for json_file in json_file_list:\n            dir_name = dir.split('\\\\')[-1].replace('.dav', '.avi')\n            new_dir_name = os.path.join(new_path, dir_name)\n            if not os.path.exists(new_dir_name):\n                os.mkdir(new_dir_name)\n            shutil.copy(json_file, new_dir_name)\n            newdata = read_json(new_dir_name + '\\\\' + os.path.basename(json_file))\n            write_json(new_dir_name + '\\\\' + os.path.basename(json_file), newdata)\n        other_files = []\n        for root, dirs, files in os.walk(old_path):\n            for file_name in files:\n                if file_name.endswith('.jpg') or file_name.endswith('.avi'):\n                    other_files.append(os.path.join(old_path, file_name))\n        for file in other_files:\n            shutil.copy(file, new_path)\n\n# \u4e3b\u51fd\u6570\nif __name__ == \"__main__\":\n    old_path = r'd:\\ss'\n    new_path = r'd:\\json'\n    copy_files(old_path, new_path)<\/pre>\n<p><strong>eof \u9519\u8bef\u89e3\u51b3<\/strong><\/p>\n<p>eof \u9519\u8bef\u662f\u56e0\u4e3a\u5c1d\u8bd5\u5728\u6587\u4ef6\u672b\u5c3e\u6267\u884c eval(json_data)\uff0c\u800c\u6b64\u65f6 json_data \u4e3a\u7a7a\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5 json_data \u662f\u5426\u4e3a\u7a7a\u6765\u907f\u514d\u6b64\u9519\u8bef\u3002<\/p>\n<pre>import os\nimport json\n\nfile_path = r'F:\\vscode_pj\\pj_test\\test_data\\1\\2.json'\n\nwith open(file_path) as f:\n    data = f.read()\n    if data:\n        b = eval(data)<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 Python \u4ee3\u7801\u4fee\u6539 JSON \u6587\u4ef6\u4e2d\u7684\u5b57\u6bb5\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ \u4fee\u6539 json \u548c\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5230\u65b0\u8def\u5f84 \u4ee5\u4e0b python \u4ee3\u7801\u53ef\u7528\u4e8e\u4fee\u6539 json \u6587\u4ef6\u4e2d\u7684\u90e8\u5206\u5b57\u6bb5\uff0c\u5e76\u590d\u5236\u6587\u4ef6\u5939\u4e2d\u7684 json \u6587\u4ef6\u3001jpg \u6587\u4ef6\u548c avi \u6587\u4ef6\u5230\u65b0\u8def\u5f84\u3002 import os import json import shutil # \u83b7\u53d6 json \u6587\u4ef6\u7684\u8def\u5f84 def get_json_data(old_json_path): json_file_paths = [] for root, dirs, files in os.walk(old_json_path): for file_name in files: if file_name.endswith(&#8216;.json&#8217;): old_json_file_path = os.path.join(root, file_name) json_file_paths.append(old_json_file_path) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50772","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50772"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50772\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}