python - 将json文件读入python - 错误:期望值:第1行第1列(char 0)
问题描述
我有一个 json 文件,其中包含来自 Twitter API 的 500 条推文。它们存储为 .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 不支持评论。在此处阅读更多信息:Cancomments are used in 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/
有关 bson 的更多详细信息,请阅读 Daniel Roseman 的回答。
推荐阅读
- c# - 如何添加自定义声明类型
- z3 - z3中实际值计算的问题
- database - Jmeter脚本开发:IBM cloudant性能测试,最大请求/秒
- flask - flask_mail 似乎是在 pythonanywhere 中导入的,但是运行时 get error code: no module named
- javascript - 切换大小写自动缩进以启用 VSCode 上的折叠
- c++ - Cpp 在不需要时要求默认构造函数
- r - 覆盖 left_join dplyr 以更新数据
- java - 使用 JDBC 将数据插入数据库的问题
- nuget - Azure Pipelines NuGet.config 包无法还原
- python - Xgboost 与简单的时间序列数据框 Python