英文:
convert code written in python to be compatible with jq command
问题
我有这段Python代码可以工作。但我想使用jq命令行实用程序,以便它可以成为shell脚本的一部分。
import pandas as pd
import json
with open('output.txt') as f:
x = json.load(f)
df = pd.DataFrame(x['Datapoints'])
df.sort_values('Timestamp')
更新:
我得到了正确的输出,如下所示:
$ cat output.txt | jq '.Datapoints[] | {cidr: .Timestamp, comment: .Average} | join(",")' | more
"2023-06-12T08:19:00+00:00,0.18333333333333332"
"2023-06-12T04:10:00+00:00,0.03333333333333333"
"2023-06-12T07:42:00+00:00,0.03333333333333333"
"2023-06-12T03:33:00+00:00,0"
但我不需要双引号,第二个字段应为0.18,而不是0.1833。
更新2:
# head -20 output.txt
{
"Label": "DBLoad",
"Datapoints": [
{
"Timestamp": "2023-06-12T08:19:00+00:00",
"Average": 0.18333333333333332,
"Unit": "None"
},
{
"Timestamp": "2023-06-12T04:10:00+00:00",
"Average": 0.03333333333333333,
"Unit": "None"
},
{
"Timestamp": "2023-06-12T07:42:00+00:00",
"Average": 0.03333333333333333,
"Unit": "None"
},
{
"Timestamp": "2023-06-12T03:33:00+00:00",
英文:
I have this python code that works.
But I will like to use jq command line utility so that it can be part of shell script.
import pandas as pd
import json
with open('output.txt') as f:
x = json.load(f)
df = pd.DataFrame(x['Datapoints'])
df.sort_values('Timestamp')
Update:
I am getting correct output as shown below:
$ cat output.txt | jq ' .Datapoints[] | {cidr: .Timestamp, comment: .Average} | join(",")' | more
"2023-06-12T08:19:00+00:00,0.18333333333333332"
"2023-06-12T04:10:00+00:00,0.03333333333333333"
"2023-06-12T07:42:00+00:00,0.03333333333333333"
"2023-06-12T03:33:00+00:00,0"
But I do not need double quotes and second field should be 0.18 instead of 0.1833
Update 2:
# head -20 output.txt
{
"Label": "DBLoad",
"Datapoints": [
{
"Timestamp": "2023-06-12T08:19:00+00:00",
"Average": 0.18333333333333332,
"Unit": "None"
},
{
"Timestamp": "2023-06-12T04:10:00+00:00",
"Average": 0.03333333333333333,
"Unit": "None"
},
{
"Timestamp": "2023-06-12T07:42:00+00:00",
"Average": 0.03333333333333333,
"Unit": "None"
},
{
"Timestamp": "2023-06-12T03:33:00+00:00",
答案1
得分: 2
你想在一个已排序的数组上使用@csv
过滤器,可能还要对数字进行四舍五入,如果我理解你的问题正确的话。
jq -r '.Datapoints
| sort_by(.Timestamp)[]
| [.Timestamp, (.Average*100|round/100)]
| @csv' output.txt
示例输出:
"2023-06-12T04:10:00+00:00",0.03
"2023-06-12T07:42:00+00:00",0.03
"2023-06-12T08:19:00+00:00",0.18
英文:
You want to use the @csv
filter with -r
/--raw-output
on a sorted array (and probably apply rounding to numbers, if I read your question right)
jq -r '.Datapoints
| sort_by(.Timestamp)[]
| [.Timestamp, (.Average*100|round/100)]
| @csv' output.txt
Example output:
"2023-06-12T04:10:00+00:00",0.03
"2023-06-12T07:42:00+00:00",0.03
"2023-06-12T08:19:00+00:00",0.18
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论