前提
プログラミング初心者の学生です
やりたいこと
jsonファイルをcsvファイルに変換したいのですが,以下のファイルを実行するとエラーは出ないけど,新しいcsvファイルができていない感じです...
理由がわからないので,教えていただけると嬉しいです...
python
1import pandas as pd 2import json 3import os 4import glob 5 6path = "" 7files = glob.glob(path + "*") 8 9for num, filename in enumerate(files): 10 try: 11 with open(os.path.join(os.getcwd(), filename), 'r') as f: 12 data = json.loads(f.read()) 13 14 df = pd.json_normalize(data["data"]) 15 16 #df = pd.concat(df, pd.DataFrame) 17 coords = df["geo.coordinates.coordinates"].apply(pd.Series) 18 coords.columns = ["x", "y"] 19 df = pd.concat([df, coords], axis=1) 20 21 df = df.drop(["entities", "conversation_id", "geo.place_id", "public_metrics", "author_id", "source", "reply_settings", "lang", "possibly_sensitive", "id"], axis=1) 22 23 #書き出し 24 df.to_csv("" + "ooinunohuguri_" + str(num+1) + ".csv") 25 except Exception: 26 pass
元のjsonファイル
json
1{ 2 "data": [ 3 { 4 "entities": { 5 "hashtags": [ 6 { 7 "start": 4, 8 "end": 8, 9 "tag": "\u3064\u304f\u3057" 10 }, 11 { 12 "start": 58, 13 "end": 64, 14 "tag": "\u3042\u3093\u305a\u306e\u82b1" 15 }, 16 { 17 "start": 79, 18 "end": 82, 19 "tag": "\u9577\u91ce" 20 }, 21 { 22 "start": 83, 23 "end": 86, 24 "tag": "\u4fe1\u5dde" 25 }, 26 { 27 "start": 87, 28 "end": 89, 29 "tag": "\u6625" 30 }, 31 { 32 "start": 90, 33 "end": 93, 34 "tag": "\u5b63\u7bc0" 35 }, 36 { 37 "start": 94, 38 "end": 97, 39 "tag": "\u5e78\u305b" 40 }, 41 { 42 "start": 98, 43 "end": 105, 44 "tag": "spring" 45 }, 46 { 47 "start": 106, 48 "end": 116, 49 "tag": "instalike" 50 }, 51 { 52 "start": 117, 53 "end": 124, 54 "tag": "nagano" 55 }, 56 { 57 "start": 125, 58 "end": 131, 59 "tag": "japan" 60 } 61 ], 62 "urls": [ 63 { 64 "start": 163, 65 "end": 186, 66 "url": "https://t.co/i0m9e2aNqx", 67 "expanded_url": "https://www.instagram.com/p/BwPNF--Aq2O/?utm_source=ig_twitter_share&igshid=18vkm5stcye0m", 68 "display_url": "instagram.com/p/BwPNF--Aq2O/\u2026" 69 } 70 ] 71 }, 72 "conversation_id": "1117418789384204288", 73 "geo": { 74 "coordinates": { 75 "type": "Point", 76 "coordinates": [ 77 138.12, 78 36.5339 79 ] 80 }, 81 "place_id": "89dfbb2b907fec3b" 82 }, 83 "public_metrics": { 84 "retweet_count": 0, 85 "reply_count": 0, 86 "like_count": 1, 87 "quote_count": 0 88 }, 89 "author_id": "994839413808123904", 90 "source": "Instagram", 91 "text": "\u4eca\u65e5\u306f #\u3064\u304f\u3057 \u898b\u3064\u3051\u3066\u30c6\u30f3\u30b7\u30e7\u30f3\u4e0a\u304c\u3063\u305f\ud83d\ude3b\u2934\ufe0e\u2934\ufe0e\n\u6625\u3092\u611f\u3058\u308b#\u30aa\u30aa\u30a4\u30cc\u30ce\u30d5\u30b0\u30ea \u3082\u5927\u597d\u304d\u306a\u82b1\u266a\n\u6c17\u306b\u306a\u3063\u3066\u305f #\u3042\u3093\u305a\u306e\u82b1 \u3082\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\ud83d\udc40\u2728\n#\u9577\u91ce #\u4fe1\u5dde #\u6625 #\u5b63\u7bc0 #\u5e78\u305b #spring #instalike #nagano #japan \u5834\u6240: Chikuma-shi, Nagano, Japan https://t.co/i0m9e2aNqx", 92 "reply_settings": "everyone", 93 "lang": "ja", 94 "possibly_sensitive": false, 95 "id": "1117418789384204288", 96 "created_at": "2019-04-14T13:26:14.000Z" 97 },
回答1件
あなたの回答
tips
プレビュー