将用Python编写的代码转换为与jq命令兼容。

huangapple go评论59阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月12日 18:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定