![](/img/trans.png)
[英]Error reading json file in python Expecting value: line 1 column 1 (char 0)
[英]Reading json file into python - Error: Expecting value: line 1 column 1 (char 0)
我從Twitter API獲得了一個包含500條推文的json文件。 它們存儲為.json,我需要將它們加載到python中。
我嘗試了多種加載/讀取文件的方式,但是沒有任何效果。 文件可能有問題嗎?
我得到的錯誤是:
期望值:第1行第1列(字符0)
這是我嘗試過的許多方法之一:
file = 'resources/data/raw/500_random_tweets.json'
try:
with open(file, 'r', encoding="utf8") as myfile:
data = myfile.read()
tweets = json.loads(data)
except Exception as e:
print(e)
這是json的樣子:
/* 1 */
{
"_id" : ObjectId("5abffac15374a8000feef65c"),
"created_at" : ISODate("2018-03-31T21:16:49.123Z"),
"id" : NumberLong(980192203329990656),
"id_str" : "980192203329990656",
"text" : "RT @skychainglobal: Don't miss the #last #chance to buy Skychain tokens with a 25% discount! Just 53,877 SKCH tokens left!\n#skychain #skych…",
"source" : "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
"truncated" : false,
"in_reply_to_status_id" : null,
"in_reply_to_status_id_str" : null,
"in_reply_to_user_id" : null,
"in_reply_to_user_id_str" : null,
"in_reply_to_screen_name" : null,
"user" : {
"id" : NumberLong(926157367787638784),
"id_str" : "926157367787638784",
"name" : "Dangerouslady #TraXionICO",
"screen_name" : "I_Demonangel",
"location" : "0x5c58e15bE26f8886895350B9d1Bf1fAD53c5C83B",
"url" : null,
"description" : "0x5c58e15bE26f8886895350B9d1Bf1fAD53c5C83B",
"translator_type" : "none",
"protected" : false,
"verified" : false,
"followers_count" : 3049,
"friends_count" : 4959,
"listed_count" : 3,
"favourites_count" : 1781,
"statuses_count" : 3257,
"created_at" : "Thu Nov 02 18:41:38 +0000 2017",
"utc_offset" : null,
"time_zone" : null,
"geo_enabled" : false,
"lang" : "en",
"contributors_enabled" : false,
"is_translator" : false,
"profile_background_color" : "F5F8FA",
"profile_background_image_url" : "",
"profile_background_image_url_https" : "",
"profile_background_tile" : false,
"profile_link_color" : "1DA1F2",
"profile_sidebar_border_color" : "C0DEED",
"profile_sidebar_fill_color" : "DDEEF6",
"profile_text_color" : "333333",
"profile_use_background_image" : true,
"profile_image_url" : "http://pbs.twimg.com/profile_images/973129911836782594/DXw4iXIf_normal.jpg",
"profile_image_url_https" : "https://pbs.twimg.com/profile_images/973129911836782594/DXw4iXIf_normal.jpg",
"profile_banner_url" : "https://pbs.twimg.com/profile_banners/926157367787638784/1520847227",
"default_profile" : true,
"default_profile_image" : false,
"following" : null,
"follow_request_sent" : null,
"notifications" : null
},
"geo" : null,
"coordinates" : null,
"place" : null,
"contributors" : null,
"retweeted_status" : {
"created_at" : "Sat Mar 31 12:06:59 +0000 2018",
"id" : NumberLong(980053845496815616),
"id_str" : "980053845496815616",
"text" : "Don't miss the #last #chance to buy Skychain tokens with a 25% discount! Just 53,877 SKCH tokens left!\n#skychain… ,
"display_text_range" : [
0,
140
],
"source" : "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>",
"truncated" : true,
"in_reply_to_status_id" : null,
"in_reply_to_status_id_str" : null,
"in_reply_to_user_id" : null,
"in_reply_to_user_id_str" : null,
"in_reply_to_screen_name" : null,
"user" : {
"id" : NumberLong(935520196865019905),
"id_str" : "935520196865019905",
"name" : "Skychain",
"screen_name" : "skychainglobal",
"location" : "Москва, Россия",
"url" : "https://skychain.global/",
"description" : "Skychain is an open infrastructure, blockchain project\naimed to host, train and use\nmedical neural networks.",
"translator_type" : "none",
"protected" : false,
"verified" : false,
"followers_count" : 4848,
"friends_count" : 0,
"listed_count" : 114,
"favourites_count" : 34,
"statuses_count" : 125,
"created_at" : "Tue Nov 28 14:46:10 +0000 2017",
"utc_offset" : -25200,
"time_zone" : "Pacific Time (US & Canada)",
"geo_enabled" : false,
"lang" : "ru",
"contributors_enabled" : false,
"is_translator" : false,
"profile_background_color" : "000000",
"profile_background_image_url" : "http://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_image_url_https" : "https://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_tile" : false,
"profile_link_color" : "1B95E0",
"profile_sidebar_border_color" : "000000",
"profile_sidebar_fill_color" : "000000",
"profile_text_color" : "000000",
"profile_use_background_image" : false,
"profile_image_url" : "http://pbs.twimg.com/profile_images/945202909242896384/OUsbpDAO_normal.jpg",
"profile_image_url_https" : "https://pbs.twimg.com/profile_images/945202909242896384/OUsbpDAO_normal.jpg",
"profile_banner_url" : "https://pbs.twimg.com/profile_banners/935520196865019905/1514189033",
"default_profile" : false,
"default_profile_image" : false,
"following" : null,
"follow_request_sent" : null,
"notifications" : null
},
"geo" : null,
"coordinates" : null,
"place" : null,
"contributors" : null,
"is_quote_status" : false,
"extended_tweet" : {
"full_text" : "Don't miss the #last #chance to buy Skychain tokens with a 25% discount! Just 53,877 SKCH tokens left!\n#skychain #skychainglobal #ico #medicine #blockchain ...",
"display_text_range" : [
0,
155
],
"entities" : {
"hashtags" : [
{
"text" : "last",
"indices" : [
15,
20
]
},
{
"text" : "chance",
"indices" : [
21,
28
]
},
{
"text" : "skychain",
"indices" : [
103,
112
]
},
{
"text" : "skychainglobal",
"indices" : [
113,
128
]
},
{
"text" : "ico",
"indices" : [
129,
133
]
},
{
"text" : "medicine",
"indices" : [
134,
143
]
},
{
"text" : "blockchain",
"indices" : [
144,
155
]
}
],
"urls" : [],
"user_mentions" : [],
"symbols" : [],
"media" : [
{
"id" : NumberLong(980053810260467712),
"id_str" : "980053810260467712",
"indices" : [
156,
179
],
"media_url" : "http://pbs.twimg.com/media/DZnZv3VX4AAlflW.png",
"media_url_https" : "https://pbs.twimg.com/media/DZnZv3VX4AAlflW.png",
"url" : "...",
"display_url" : "pic.twitter.com/68Ma6BljIX",
"expanded_url" : "https://twitter.com/skychainglobal/status/980053845496815616/photo/1",
"type" : "photo",
"sizes" : {
"medium" : {
"w" : 256,
"h" : 256,
"resize" : "fit"
},
"small" : {
"w" : 256,
"h" : 256,
"resize" : "fit"
},
"large" : {
"w" : 256,
"h" : 256,
"resize" : "fit"
},
"thumb" : {
"w" : 150,
"h" : 150,
"resize" : "crop"
}
}
}
]
},
"extended_entities" : {
"media" : [
{
"id" : NumberLong(980053810260467712),
"id_str" : "980053810260467712",
"indices" : [
156,
179
],
"media_url" : "http://pbs.twimg.com/media/DZnZv3VX4AAlflW.png",
"media_url_https" : "https://pbs.twimg.com/media/DZnZv3VX4AAlflW.png",
"url" : "...",
"display_url" : "pic.twitter.com/68Ma6BljIX",
"expanded_url" : "https://twitter.com/skychainglobal/status/980053845496815616/photo/1",
"type" : "photo",
"sizes" : {
"medium" : {
"w" : 256,
"h" : 256,
"resize" : "fit"
},
"small" : {
"w" : 256,
"h" : 256,
"resize" : "fit"
},
"large" : {
"w" : 256,
"h" : 256,
"resize" : "fit"
},
"thumb" : {
"w" : 150,
"h" : 150,
"resize" : "crop"
}
}
}
]
}
},
"quote_count" : 7,
"reply_count" : 4,
"retweet_count" : 450,
"favorite_count" : 391,
"entities" : {
"hashtags" : [
{
"text" : "last",
"indices" : [
15,
20
]
},
{
"text" : "chance",
"indices" : [
21,
28
]
},
{
"text" : "skychain",
"indices" : [
103,
112
]
}
],
"urls" : [
{
"url" : "...",
"expanded_url" : "https://twitter.com/i/web/status/980053845496815616",
"display_url" : "twitter.com/i/web/status/9…",
"indices" : [
114,
137
]
}
],
"user_mentions" : [],
"symbols" : []
},
"favorited" : false,
"retweeted" : false,
"possibly_sensitive" : false,
"filter_level" : "low",
"lang" : "en"
},
"is_quote_status" : false,
"quote_count" : 0,
"reply_count" : 0,
"retweet_count" : 0,
"favorite_count" : 0,
"entities" : {
"hashtags" : [
{
"text" : "last",
"indices" : [
35,
40
]
},
{
"text" : "chance",
"indices" : [
41,
48
]
},
{
"text" : "skychain",
"indices" : [
123,
132
]
}
],
"urls" : [],
"user_mentions" : [
{
"screen_name" : "skychainglobal",
"name" : "Skychain",
"id" : NumberLong(935520196865019905),
"id_str" : "935520196865019905",
"indices" : [
3,
18
]
}
],
"symbols" : []
},
"favorited" : false,
"retweeted" : false,
"filter_level" : "low",
"lang" : "en",
"timestamp_ms" : "1522531007021",
"search_id" : "ai_blockchain"
}
/* 2 */
{
"_id" : ObjectId("5abffac15374a8000feef65d"),
"created_at" : ISODate("2018-03-31T21:16:49.571Z"),
"id" : NumberLong(980192209034100736),
"id_str" : "980192209034100736",
"text" : "RT @Waltonchain: We are glad to see our Waltonchain Windows Wallet is well received by the community upon launch. It's an effortless one-cl…",
"source" : "<a href=\"https://t.me/alt_time\" rel=\"nofollow\"> HJ's Coin Crawler 3</a>",
"truncated" : false,
"in_reply_to_status_id" : null,
"in_reply_to_status_id_str" : null,
"in_reply_to_user_id" : null,
"in_reply_to_user_id_str" : null,
"in_reply_to_screen_name" : null,
"user" : {
"id" : NumberLong(959659082956156928),
"id_str" : "959659082956156928",
"name" : "How to Coin",
"screen_name" : "how_to_coin",
...
JSON不支持注釋。 在這里閱讀有關它的更多信息: 注釋可以在JSON中使用嗎?
就您而言,您應該只刪除文件開頭的/* 1 */
。 那可行。
我注意到您的文件中有多個json對象。 每個對象都標記有這些注釋之一。 您應該在這些位置拆分文件,以確保將所有不同的json對象也加載到不同的python對象中。
這是一個正則表達式示例,用於將文件拆分為不同的json對象:
import re
re.split("/\* [0-9]+ \*/\n", data)
此正則表達式在文件中具有注釋的格式。
編輯 :我注意到您的文件實際上並不包含真實的json數據。 可能是bson。 您可以使用工具“ bsondump”將其轉換為真實的json: https ://docs.mongodb.com/manual/reference/program/bsondump/
閱讀Daniel Roseman的答案以獲取有關bson的更多詳細信息。
該文件不是JSON。 它不僅以注釋開頭,還包含幾種類型-ObjectId,NumberLong,ISODate-JSON也不支持。
相反,它似乎是BSON ,這是MongoDb使用的序列化格式。 您需要從MongoDb庫安裝BSON客戶端才能讀取它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.