gpt4 book ai didi

python - 如何在Python中根据json文件的最新时间戳条件创建并写入文件?

转载 作者:太空宇宙 更新时间:2023-11-03 21:44:08 25 4
gpt4 key购买 nike

这是我拥有的大 json 文件的一部分。我正在使用 yamlPath 中的内容创建文件夹,然后将 sqlQuery 中的内容写入文件。但在重复“yamlName+yamlPath”的情况下,我的代码仅使用第一个结果创建文件,并插入从 for 循环获取的 sqlQuery 内容。因此,在这种重复场景中,我只想选取 sqlQuery 并根据最新/最大 jobEndTimestamp 创建一个文件。

如何使代码仅获取最新的 jobEndTimestamp 并创建文件?

预期输出:

/app/computer/users/ship-notice-data.sql   -> select from table.b
/app/computer/a/users/boat-notice-data.sql -> select from table.b
{
"stream": [
{
"applicationServiceId": "uhhj",
"yamlName": "/users/ship-notice-data.yml",
"yamlPath": "/app/computer",
"jobStartTimestamp": "2018-09-15 04:12:46",
"jobEndTimestamp": "2018-09-15 04:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.a"
},{
"applicationServiceId": "uhhj",
"yamlName": "/users/ship-notice-data.yml",
"yamlPath": "/app/computer",
"jobStartTimestamp": "2018-09-15 21:12:46",
"jobEndTimestamp": "2018-09-15 21:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.b"
},{
"applicationServiceId": "uhhj",
"yamlName": "/users/car-notice-data.yaml",
"yamlPath": "/app/computer/s",
"jobStartTimestamp": "2018-09-15 04:12:46",
"jobEndTimestamp": "2018-09-15 06:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.b"
},{
"applicationServiceId": "uhhj",
"yamlName": "/users/boat-notice-data.yaml",
"yamlPath": "/app/computer/a",
"jobStartTimestamp": "2018-09-15 04:12:46",
"jobEndTimestamp": "2018-09-15 06:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.b"
}
]
}

这是我的代码:

with open('/Users/mona/stream.json', 'r') as f:
item_dict = json.load(f)

for item in item_dict['stream']:
if (item['applicationServiceId'] == 'uhhj' and
item.get('targetHostName') == 'DB2' and
(item['targetType'] == 'DB2')):
# print(item.get('applicationServiceId'))
v3 = item.get('applicationServiceId')
v4 = item.get('jobEndTimestamp')
v = item.get('sqlQuery')
v1 = item.get('yamlName')
v2 = item.get('yamlPath')
print(v1+v2+" "+v4+ " " +str(v))
# v4 = str(item.get('yamlName').split('/')[-1].split('.')[0])
# print(v4)

originalPath = "/Users/mona/"

fullPath = os.path.join(originalPath+v2+(v1.split('/')[1].split('/')[0])+'/'+(v1.split('/')[2].split('/')[0])+'/')
# print(fullPath)
os.makedirs(fullPath, mode=0o777, exist_ok=True)
# print(v1)

with open(fullPath + str(item.get('yamlName').split('/')[-1].split('.')[0]) + ".sql", "w") as newFile:
newFile.write("%s \n" % (v))

最佳答案

也许这个解决方案不是最优的,但它可以工作......

这个想法是通过 yamlPath 对字典进行分组,每个路径都包含一个带有 yamlPath 的字典列表。

然后,您拆分并重新加入路径,并保存名称(因此它与您的操作系统兼容),然后,最后,我只是打开上下文,以便您可以附加到您想要的文件只需打开(如果不存在则创建,或者如果存在则打开并追加,这就是 'a' 参数的原因)。

from collections import defaultdic
import os
objects = defaultdic(list)

for item in item_dict['stream']:
objects[item.yamlPath].append(item)
for yamlPath in objects.keys():
for item in yamlPath:
temp = yamlPath.split("/") + item.yamlName.split("/")[:-1]
name = item.yamlName.split("/")[-1]
file_with_path = os.path.join(*temp)
with open(os.path.join(*temp, name), 'a') as file:
pass # Do what ever to the content

同样,这只是一个可能有效的想法,(还没有尝试过)并且您在创建目录时遇到困难,但如果问题仍然是时间戳,请遵循与此相同的逻辑,并将它们分组按您想要的时间戳,然后迭代 max(objects.keys()) (假设这些日期即使不只是转换它们也是可比较的)

关于python - 如何在Python中根据json文件的最新时间戳条件创建并写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com