我具有某个功能的输出,看起来像这样(这是Google Analytics(分析)数据,向我提供了有关我网站用户的反馈)
{'sampleRate': 1,
# Pageview activity
'sessions': [{'activityTime': '2020-01-08T15:48:38.012671Z',
'activityType': 'PAGEVIEW',
'campaign': '(not set)',
'channelGrouping': 'Direct',
'customDimension': [{'index': 1}],
'hostname': 'company.domain.com',
'keyword': '(not set)',
'landingPagePath': '/login',
'medium': '(none)',
'pageview': {'pagePath': '/login',
'pageTitle': 'titleofthepage'},
'source': '(direct)'},
# Event activity
{'activityTime': '2020-01-08T15:48:37.915105Z',
'activityType': 'EVENT',
'campaign': '(not set)',
'channelGrouping': 'Direct',
'customDimension': [{'index': 1}],
'event': {'eventAction': 'Successfully Logged '
'In',
'eventCategory': 'Auth',
'eventCount': '1',
'eventLabel': '(not set)'},
'hostname': 'company.domain.com',
'keyword': '(not set)',
'landingPagePath': '/login',
'medium': '(none)',
'source': '(direct)'}]
'dataSource': 'web',
'deviceCategory': 'desktop',
'platform': 'Windows',
'sessionDate': '2020-01-08',
关于活动的注意事项:活动的数量并不总是两个,有时用户执行了20个活动,而有时他们仅执行了1个活动,所以我给出的输出尽可能地简单(活动被分类为“浏览量”或“事件”,但绝不能同时出现)
关于原始功能的注意事项:原始功能(提供GA输出的功能)正在for循环中运行,对于每个用户,所有用户数据都存储在一个大列表中。需要明确的是,以上数据仅适用于一位用户。
期望的输出:我想要的是一个有两个表的数据库(如下所示)。我不确定如何为许多需要帮助的用户执行此操作。我希望能够参加会议并向我们的用户提供反馈。
表1:
SessionId User dataSource deviceCategory platform sessionDuration
12345 123 web desktop windows 00:15:12
...
表2:
ActivityTime pageTitle pagePath EventCategory eventCount eventLabel eventAction
2019-12-15 20:30:12 domain webpage NaN NaN NaN NaN
2019-12-15 20:45:47 NaN NaN Aut 1 (not_set) LoggedIn
PS:我知道这似乎很复杂,但很简单:我得到了一个嵌套字典列表,我希望使用Peewee将其放入数据库中,以便能够进行查询。
如果我误会了一些东西,请告诉我
我认为PRAGMA
可以解决问题,然后我应该知道如何将所有这些数据存储为.db文件(我认为)?
谢谢:)
答案 0 :(得分:0)
我建议将这些数据存储为JSON文件,并使用内置的json
模块加载此数据,然后执行您想做的任何事情:
您的JSON:
{
"sampleRate": 1,
"sessions": [
{
"activityTime": "2020-01-08T15:48:38.012671Z",
"activityType": "PAGEVIEW",
"campaign": "(not set)",
"channelGrouping": "Direct",
"customDimension": [
{
"index": 1
}
],
"hostname": "company.domain.com",
"keyword": "(not set)",
"landingPagePath": "/login",
"medium": "(none)",
"pageview": {
"pagePath": "/login",
"pageTitle": "titleofthepage"
},
"source": "(direct)"
},
{
"activityTime": "2020-01-08T15:48:37.915105Z",
"activityType": "EVENT",
"campaign": "(not set)",
"channelGrouping": "Direct",
"customDimension": [
{
"index": 1
}
],
"event": {
"eventAction": "Successfully Logged In",
"eventCategory": "Auth",
"eventCount": "1",
"eventLabel": "(not set)"
},
"hostname": "company.domain.com",
"keyword": "(not set)",
"landingPagePath": "/login",
"medium": "(none)",
"source": "(direct)"
}
],
"dataSource": "web",
"deviceCategory": "desktop",
"platform": "Windows",
"sessionDate": "2020-01-08"
}
Python代码:
import json
with open('data.json', 'r', encoding='utf-8') as fw:
obj = json.load(fw) # your nested dictionary
# peewee mapping
# or
# db insertion
# or
# some other stuff
pass