您的 json 文件不是正确的 json 文件。
您需要在文件开头添加“[”,在文件末尾添加“]”,并用逗号分隔每个“{}”部分。
这是一个例子:
数据.json
[
{
"ts": 1393631983,
"visitor_uuid": "ade7e1f63bc83c66",
"visitor_source": "external",
"visitor_device": "browser",
"visitor_useragent": "Opera/9.80 (Windows NT 6.1) Presto/2.12.388 Version/12.16",
"visitor_ip": "b5af0ba608ab307c",
"visitor_country": "BR",
"visitor_referrer": "53c643c16e8253e7",
"env_type": "reader",
"env_doc_id": "140222143932-91796b01f94327ee809bd759fd0f6c76",
"event_type": "pagereadtime",
"event_readtime": 1010,
"subject_type": "doc",
"subject_doc_id": "140222143932-91796b01f94327ee809bd759fd0f6c76",
"subject_page": 3
}, {
"ts": 1393631983,
"visitor_uuid": "232eeca785873d35",
"visitor_source": "internal",
"visitor_device": "browser",
"visitor_useragent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36",
"visitor_ip": "fcf9c67037f993f0",
"visitor_country": "MX",
"visitor_referrer": "63765fcd2ff864fd",
"env_type": "stream",
"env_ranking": 10,
"env_build": "1.7.118-b946",
"env_name": "explore",
"env_component": "editors_picks",
"event_type": "impression",
"subject_type": "doc",
"subject_doc_id": "100713205147-2ee05a98f1794324952eea5ca678c026",
"subject_page": 1
}, {
"ts": 1393631983,
"visitor_uuid": "232eeca785873d35",
"visitor_source": "internal",
"visitor_device": "browser",
"visitor_useragent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36",
"visitor_ip": "fcf9c67037f993f0",
"visitor_country": "PL",
"visitor_referrer": "63765fcd2ff864fd",
"env_type": "stream",
"env_ranking": 10,
"env_build": "1.7.118-b946",
"env_name": "explore",
"env_component": "editors_picks",
"event_type": "impression",
"subject_type": "doc",
"subject_doc_id": "100713205147-2ee05a98f1794324952eea5ca678c026",
"subject_page": 1
}
, {
"ts": 1393631983,
"visitor_uuid": "232eeca785873d35",
"visitor_source": "internal",
"visitor_device": "browser",
"visitor_useragent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36",
"visitor_ip": "fcf9c67037f993f0",
"visitor_country": "PL",
"visitor_referrer": "63765fcd2ff864fd",
"env_type": "stream",
"env_ranking": 10,
"env_build": "1.7.118-b946",
"env_name": "explore",
"env_component": "editors_picks",
"event_type": "impression",
"subject_type": "doc",
"subject_doc_id": "100713205147-2ee05a98f1794324952eea5ca678c026",
"subject_page": 1
}
]
之后对于 data.json 文件中的每个元素,我正在检查它是否与我们的输入 subject_doc_id 匹配。如果我们得到匹配,我会将其附加到匹配列表中,这样我们就可以收集直方图的数据。之后,我想根据唯一国家/地区的数量获得一些垃圾箱,因此我正在创建一个唯一的国家列表,然后我正在检查它的长度。
import matplotlib.pyplot as plt
import json
with open("data.json") as json_file:
data = json.load(json_file)
#Here is the subject id i'm using for the data presentation
#100713205147-2ee05a98f1794324952eea5ca678c026
subject_id = input("subject_doc_id: ")
visitors = []
for i in range(len(data)):
if subject_id == data[i]["subject_doc_id"]:
print("got a match from {}".format(data[i]["visitor_country"]))
visitors.append(data[i]["visitor_country"])
countries = []
for i in visitors:
if i not in countries:
countries.append(i)
try:
plt.hist(visitors, bins = len(countries))
plt.show()
except ValueError:
print("No matches for given subject_doc_id")
如果要按大洲排序,首先需要知道哪个国家属于哪个大洲。我的例子:
continents = {
"europe": ["PL, GER"],
"south_america": ["BR"],
"north_america": ["MX"]
}
我是 python 新手,所以除了循环之外,我不知道任何花哨的技术来对以前的列表进行排序。
continent_data = []
for continent in continents:
for visitor_country in visitors:
for country in continents[continent]:
if visitor_country in country:
continent_data.append(continent)
print(continent_data)
之后,您可以使用前面的代码将其排序为 bin 的唯一值,并根据上面的示例创建直方图