質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2483閲覧

pythonとjson

kohekoh

総合スコア140

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/06/30 08:35

編集2017/06/30 09:10

以下のようなコードがあり
実行すると以下のようになります

python

1import json 2import time 3 4def main(): 5 locdata = [] 6 7 f = open("business.txt","r",encoding='utf-8_sig') 8 for line in f: 9 json_dict = json.loads(line) 10 locdata.append([json_dict["business_id"],json_dict["latitude"],json_dict["longitude"]]) 11 12 with open("locdata(gati).txt","w") as fw: 13 for x in locdata: 14 fw.write(json.dumps(x) + "\n") 15 16if __name__ == "__main__": 17 main() 18 19----------------------------------------------------------------- 20実行結果 21Traceback (most recent call last): 22 File "get_business.py", line 26, in <module> 23 main() 24 File "get_business.py", line 13, in main 25 json_dict = json.loads(line) 26 File "C:\Users\user\AppData\Local\conda\conda\envs\anaconda\lib\json\__init__.py", line 354, in loads 27 return _default_decoder.decode(s) 28 File "C:\Users\user\AppData\Local\conda\conda\envs\anaconda\lib\json\decoder.py", line 342, in decode 29 raise JSONDecodeError("Extra data", s, end) 30json.decoder.JSONDecodeError: Extra data: line 1 column 1340 (char 1339) 31

business.txtは

{"business_id": "5UmKMjUEUNdYWqANhGckJw", "full_address": "4734 Lebanon Church Rd\nDravosburg, PA 15034", "hours": {"Friday": {"close": "21:00", "open": "11:00"}, "Tuesday": {"close": "21:00", "open": "11:00"}, "Thursday": {"close": "21:00", "open": "11:00"}, "Wednesday": {"close": "21:00", "open": "11:00"}, "Monday": {"close": "21:00", "open": "11:00"}}, "open": true, "categories": ["Fast Food", "Restaurants"], "city": "Dravosburg", "review_count": 7, "name": "Mr Hoagie", "neighborhoods": [], "longitude": -79.9007057, "state": "PA", "stars": 3.5, "latitude": 40.3543266, "attributes": {"Take-out": true, "Drive-Thru": false, "Good For": {"dessert": false, "latenight": false, "lunch": false, "dinner": false, "brunch": false, "breakfast": false}, "Caters": false, "Noise Level": "average", "Takes Reservations": false, "Delivery": false, "Ambience": {"romantic": false, "intimate": false, "classy": false, "hipster": false, "divey": false, "touristy": false, "trendy": false, "upscale": false, "casual": false}, "Parking": {"garage": false, "street": false, "validated": false, "lot": false, "valet": false}, "Has TV": false, "Outdoor Seating": false, "Attire": "casual", "Alcohol": "none", "Waiter Service": false, "Accepts Credit Cards": true, "Good for Kids": true, "Good For Groups": true, "Price Range": 1}, "type": "business"}{"business_id": "UsFtqoBl7naz8AVUBZMjQQ", "full_address": "202 McClure St\nDravosburg, PA 15034", "hours": {}, "open": true, "categories": ["Nightlife"], "city": "Dravosburg", "review_count": 5, "name": "Clancy's Pub", "neighborhoods": [], "longitude": -79.8868138, "state": "PA", "stars": 3.0, "latitude": 40.3505527, "attributes": {"Happy Hour": true, "Accepts Credit Cards": true, "Good For Groups": true, "Outdoor Seating": false, "Price Range": 1}, "type": "business"} {"business_id": "cE27W9VPgO88Qxe4ol6y_g", "full_address": "1530 Hamilton Rd\nBethel Park, PA 15234", "hours": {}, "open": false, "categories": ["Active Life", "Mini Golf", "Golf"], "city": "Bethel Park", "review_count": 5, "name": "Cool Springs Golf Center", "neighborhoods": [], "longitude": -80.0146597, "state": "PA", "stars": 2.5, "latitude": 40.3541155, "attributes": {"Good for Kids": true}, "type": "business"} {"business_id": "mVHrayjG3uZ_RLHkLj-AMg", "full_address": "414 Hawkins Ave\nBraddock, PA 15104", "hours": {"Tuesday": {"close": "19:00", "open": "10:00"}, "Friday": {"close": "20:00", "open": "10:00"}, "Wednesday": {"close": "19:00", "open": "10:00"}, "Thursday": {"close": "19:00", "open": "10:00"}, "Saturday": {"close": "16:00", "open": "10:00"}}, "open": true, "categories": ["Bars", "American (New)", "Nightlife", "Lounges", "Restaurants"], "city": "Braddock", "review_count": 26, "name": "Emil's Lounge", "neighborhoods": [], "longitude": -79.8662107, "state": "PA", "stars": 4.5, "latitude": 40.4088301, "attributes": {"Alcohol": "full_bar", "Noise Level": "average", "Has TV": true, "Attire": "casual", "Ambience": {"romantic": false, "intimate": false, "classy": false, "hipster": false, "divey": true, "touristy": false, "trendy": false, "upscale": false, "casual": false}, "Good for Kids": true, "Price Range": 2, "Good For Dancing": false, "Delivery": false, "Coat Check": false, "Smoking": "no", "Accepts Credit

このようなデータです

なぜエラーになるのでしょうか

追記
このデータだと実行することができます

python

1{"user_id": "ZxdojCZgZhw36xuSGaryRg", "text": "Pizza is garbage. Hoagies are excellent.", "business_id": "5UmKMjUEUNdYWqANhGckJw", "likes": 0, "date": "2016-07-17", "type": "tip"} 2{"user_id": "-6rEfobYjMxpUWLNxszaxQ", "text": "Don't waste your time.", "business_id": "cE27W9VPgO88Qxe4ol6y_g", "likes": 0, "date": "2013-04-18", "type": "tip"} 3{"user_id": "5OumCBQ_MyQsltSdbMyunA", "text": "Not easy to find. Be sure to put in directions for Rankin Police Department or you will end up at a residential home on the wrong Hawkins Ave. Found it after the third try. Also, it closes at 7pm.", "business_id": "mVHrayjG3uZ_RLHkLj-AMg", "likes": 0, "date": "2016-02-22", "type": "tip"} 4{"user_id": "EZ0r9dKKtEGVx2CdnowPCw", "text": "Your GPS will not allow you to find this place. Put Rankin police department in instead. They are directly across the street.", "business_id": "mVHrayjG3uZ_RLHkLj-AMg", "likes": 1, "date": "2013-01-06", "type": "tip"} 5{"user_id": "xb6zEQCw9I-Gl0g06e1KsQ", "text": "Great drink specials!", "business_id": "KayYbHCt-RkbGcPdGOThNg", "likes": 0, "date": "2013-12-03", "type": "tip"} 6

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

"business"}{"business_id":

という部分がありますね。(テキストで検索してみてください)
そこてJSONオブジェクトが終わっているのに後続の文字があるのがおかしいです。

投稿2017/06/30 08:47

quickquip

総合スコア11038

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kohekoh

2017/06/30 09:11

追記したデータも同じような形ですが 使うことができました
miyahan

2017/06/30 10:27

それはおそらく夢かなにかだと思います。 ともかく提示されたJSONは quiqui さんの指摘通り壊れています。 単純に、1行目1340列目で改行を入れ忘れていると思われます。
kohekoh

2017/06/30 10:37

夢でした とりあえず1行目を抜いたらできました ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問