【发布时间】:2018-09-14 12:03:47
【问题描述】:
我的目的是将 JSON 文件转换为 CSV 文件。但json文件的特殊性在于依次由几个json组成。正如我在这里展示的,一个带有两条 json 行的 json 文件。
{"node": {"id": "1097084148638274164", "__typename": "GraphImage", "edge_media_to_caption": {"edges": [{"node": {"text": "Detalle de la fachada de nuestro Ayuntamiento #Utrera #ayuntamientodeutrera #igersutrera"}}]}, "shortcode": "85oDh4S9J0", "edge_media_to_comment": {"count": 0}, "comments_disabled": false, "taken_at_timestamp": 1445002643, "dimensions": {"height": 608, "width": 1080}, "display_url": "links", "edge_media_preview_like": {"count": 13}, "owner": {"biography": "Cuenta oficial del Ayuntamiento de Utrera #UtreraSeMueve", "blocked_by_viewer": false, "country_block": false, "external_url": "links", "external_url_linkshimmed": "links", "edge_followed_by": {"count": 2819}, "followed_by_viewer": false, "edge_follow": {"count": 1317}, "follows_viewer": false, "full_name": "Ayuntamiento de Utrera", "has_channel": false, "has_blocked_viewer": false, "highlight_reel_count": 5, "has_requested_viewer": false, "id": "1969879851", "is_business_account": true, "is_private": false, "is_verified": false, "edge_mutual_followed_by": {"count": 0, "edges": []}, "profile_pic_url": "links", "profile_pic_url_hd": "links", "requested_by_viewer": false, "username": "instautrera", "connected_fb_page": null, "iphone_struct": {"pk": 1969879851, "is_private": false, "reel_auto_archive": "on", "profile_pic_id": "1756764239516976180_1969879851", "follower_count": 2819, "external_url": "http://www.utrera.org/", "biography": "Cuenta oficial del Ayuntamiento de Utrera #UtreraSeMueve", "usertags_count": 362, "hd_profile_pic_versions": [{"height": 320, "url": "links", "width": 320}, {"height": 640, "url": "links", "width": 640}], "is_verified": false, "has_highlight_reels": true, "external_lynx_url": "links", "username": "instautrera", "profile_pic_url": "links", "following_count": 1317, "media_count": 706, "hd_profile_pic_url_info": {"height": 959, "url": "links", "width": 959}, "has_anonymous_profile_picture": false, "full_name": "Ayuntamiento de Utrera", "is_potential_business": false, "auto_expand_chaining": false, "highlight_reshare_disabled": false}}, "thumbnail_src": "links", "thumbnail_resources": [{"src": "links", "config_width": 150, "config_height": 150}, {"src": "links", "config_width": 240, "config_height": 240}, {"src": "links", "config_width": 320, "config_height": 320}, {"src": "links", "config_width": 480, "config_height": 480}, {"src": "links", "config_width": 640, "config_height": 640}], "is_video": false}, "instaloader": {"version": "4.1", "node_type": "Post"}}
{"node": {"id": "1097144067710243168", "__typename": "GraphImage", "edge_media_to_caption": {"edges": [{"node": {"text": "\u00a1Qu\u00e9 buena estampa! Nuevo c\u00e9sped artificial con el Santuario de fondo.\n#deporte #Utrera #igersutrera #ayuntamientodeutrera"}}]}, "shortcode": "851rd3S9Fg", "edge_media_to_comment": {"count": 0}, "comments_disabled": false, "taken_at_timestamp": 1445009786, "dimensions": {"height": 769, "width": 1080}, "display_url": links", "edge_media_preview_like": {"count": 20}, "owner": {"biography": "Cuenta oficial del Ayuntamiento de Utrera #UtreraSeMueve", "blocked_by_viewer": false, "country_block": false, "external_url": "links", "external_url_linkshimmed": "links", "edge_followed_by": {"count": 2819}, "followed_by_viewer": false, "edge_follow": {"count": 1317}, "follows_viewer": false, "full_name": "Ayuntamiento de Utrera", "has_channel": false, "has_blocked_viewer": false, "highlight_reel_count": 5, "has_requested_viewer": false, "id": "1969879851", "is_business_account": true, "is_private": false, "is_verified": false, "edge_mutual_followed_by": {"count": 0, "edges": []}, "profile_pic_url": "links", "profile_pic_url_hd": "links", "requested_by_viewer": false, "username": "instautrera", "connected_fb_page": null, "iphone_struct": {"pk": 1969879851, "is_private": false, "reel_auto_archive": "on", "profile_pic_id": "1756764239516976180_1969879851", "follower_count": 2819, "external_url": "http://www.utrera.org/", "biography": "Cuenta oficial del Ayuntamiento de Utrera #UtreraSeMueve", "usertags_count": 362, "hd_profile_pic_versions": [{"height": 320, "url": "links", "width": 320}, {"height": 640, "url": "links", "width": 640}], "is_verified": false, "has_highlight_reels": true, "external_lynx_url": "links", "username": "instautrera", "profile_pic_url": "links", "following_count": 1317, "media_count": 706, "hd_profile_pic_url_info": {"height": 959, "url": "links", "width": 959}, "has_anonymous_profile_picture": false, "full_name": "Ayuntamiento de Utrera", "is_potential_business": false, "auto_expand_chaining": false, "highlight_reshare_disabled": false}}, "thumbnail_src": "links", "thumbnail_resources": [{"src": "links", "config_width": 150, "config_height": 150}, {"src": "links", "config_width": 240, "config_height": 240}, {"src": "links", "config_width": 320, "config_height": 320}, {"src": "links", "config_width": 480, "config_height": 480}, {"src": "links", "config_width": 640, "config_height": 640}], "is_video": false}, "instaloader": {"version": "4.1", "node_type": "Post"}}
我打算将其转换为 csv,因为两者共享相同的标题。我使用了我在互联网上看到的几个代码,但没有一个为我服务。你能指导我吗?我对 Python 还是很陌生,对如何做有点困惑。
【问题讨论】:
-
应该如何处理嵌套的 JSON?作为一个以逗号分隔的条目还是多个分隔条目?在第一种情况下使用
pandas.read_json()和pandas.DataFrame.to_csv()。在第二种情况下,您必须指定 csv 文件的外观。 -
请给出一个示例输出:)
-
"但是json文件的特殊性是依次由若干个json组成" =>这个被命名为"jsonlines",cfjsonlines.org还有一个python库:jsonlines.readthedocs.io/en/latest跨度>
-
“我使用了我在互联网上看到的几个代码,但没有一个为我服务”这确实是“复制/粘贴编程”的问题xD
-
如果您尝试将嵌套的 json 转换为 csv,这可能会有所帮助。 stackoverflow.com/a/1872081/10215730
标签: python json python-3.x csv