Python - 截断 json 数组中的时间戳
Python - Truncate a timestamp in json array
给出以下 json 数组:
[
{
"2020-02-03T00:04:11.000Z": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04T00:04:08.000Z": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05T00:04:16.000Z": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
]
我需要去掉键值的分秒毫秒
如您所见,每个项目的键值不同。
结果应该是:
[
{
"2020-02-03": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
]
此外,如果可以删除“USD”键,这3个值将直接移动到日期的第一层,那应该很棒。
您可以使用 T
作为分隔符来拆分密钥。
请考虑下一个代码:
import json
start = [
{
"2020-02-03T00:04:11.000Z": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04T00:04:08.000Z": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05T00:04:16.000Z": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
}
]
def process_data(data):
result = []
for k in data:
result += data[k]
return result
result = []
for item in start:
new_dict = {}
for dct_item in item:
data = process_data(item[dct_item])
truncated = dct_item.split('T')[0]
if truncated in new_dict:
new_dict[truncated] += data
else:
new_dict[truncated] = data
result.append(new_dict)
print(json.dumps(result,indent=4))
下面一层
data = [
{
"2020-02-03T00:04:11.000Z": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04T00:04:08.000Z": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05T00:04:16.000Z": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
}
]
short_data = {k[:k.find('T')]: v['USD'] for k, v in data[0].items()}
print(short_data)
输出
{'2020-02-03': [0.38449135, 422507.831002123, 0], '2020-02-04': [0.39151832, 390909.273872433, 0], '2020-02-05': [0.40914842, 498653.042530699, 0]}
给出以下 json 数组:
[
{
"2020-02-03T00:04:11.000Z": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04T00:04:08.000Z": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05T00:04:16.000Z": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
]
我需要去掉键值的分秒毫秒
如您所见,每个项目的键值不同。
结果应该是:
[
{
"2020-02-03": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
]
此外,如果可以删除“USD”键,这3个值将直接移动到日期的第一层,那应该很棒。
您可以使用 T
作为分隔符来拆分密钥。
请考虑下一个代码:
import json
start = [
{
"2020-02-03T00:04:11.000Z": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04T00:04:08.000Z": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05T00:04:16.000Z": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
}
]
def process_data(data):
result = []
for k in data:
result += data[k]
return result
result = []
for item in start:
new_dict = {}
for dct_item in item:
data = process_data(item[dct_item])
truncated = dct_item.split('T')[0]
if truncated in new_dict:
new_dict[truncated] += data
else:
new_dict[truncated] = data
result.append(new_dict)
print(json.dumps(result,indent=4))
下面一层
data = [
{
"2020-02-03T00:04:11.000Z": {
"USD": [
0.38449135,
422507.831002123,
0
]
},
"2020-02-04T00:04:08.000Z": {
"USD": [
0.39151832,
390909.273872433,
0
]
},
"2020-02-05T00:04:16.000Z": {
"USD": [
0.40914842,
498653.042530699,
0
]
}
}
]
short_data = {k[:k.find('T')]: v['USD'] for k, v in data[0].items()}
print(short_data)
输出
{'2020-02-03': [0.38449135, 422507.831002123, 0], '2020-02-04': [0.39151832, 390909.273872433, 0], '2020-02-05': [0.40914842, 498653.042530699, 0]}