英文:
JQ Raw Input of CSV missing first line
问题
对不起,以下是您请求的翻译部分:
"It's not clear to me why my output with this template is missing the first line of the data, what am I missing?"
JQ模板(使用--raw-data标志调用):
{ south: [inputs | split(";") | {label: .[0], date: .[1], values: .[2] | gsub("[\r]"; "") | tonumber } ]
| group_by(.label) | map({ key: (.[0].label), value: [.[] | {date: .date, values: .values}] }) | from_entries
}
如果我检查结果,第一项丢失。我不明白为什么。这不是group_by部分的问题,因为如果我删除它,它也无法获取第一行。它也不在数组中或其他任何地方,它就是丢失的。
英文:
It's not clear to me why my output with this template is missing the first line of the data, what am I missing?
Sample: https://jqplay.org/s/6VAJCkLQo9v
Raw Data:
watts;2023-02-21T06:50:00+01:00;0
watts;2023-02-21T07:00:00+01:00;1928
watts;2023-02-21T08:00:00+01:00;2686
watts;2023-02-21T09:00:00+01:00;5054
watts;2023-02-21T10:00:00+01:00;7466
watts;2023-02-21T11:00:00+01:00;7797
watts;2023-02-21T12:00:00+01:00;7520
watts;2023-02-21T13:00:00+01:00;6585
watts;2023-02-21T14:00:00+01:00;5084
watts;2023-02-21T15:00:00+01:00;3235
watts;2023-02-21T16:00:00+01:00;1345
watts;2023-02-21T17:00:00+01:00;294
watts;2023-02-21T17:31:00+01:00;0
watts;2023-02-22T06:49:00+01:00;0
watts;2023-02-22T07:00:00+01:00;2637
watts;2023-02-22T08:00:00+01:00;4629
watts;2023-02-22T09:00:00+01:00;6155
watts;2023-02-22T10:00:00+01:00;6639
watts;2023-02-22T11:00:00+01:00;6582
watts;2023-02-22T12:00:00+01:00;6124
watts;2023-02-22T13:00:00+01:00;5135
watts;2023-02-22T14:00:00+01:00;3412
watts;2023-02-22T15:00:00+01:00;1816
watts;2023-02-22T16:00:00+01:00;721
watts;2023-02-22T17:00:00+01:00;189
watts;2023-02-22T17:33:00+01:00;0
watt_hours_period;2023-02-21T06:50:00+01:00;0
watt_hours_period;2023-02-21T07:00:00+01:00;161
watt_hours_period;2023-02-21T08:00:00+01:00;2307
watt_hours_period;2023-02-21T09:00:00+01:00;3870
watt_hours_period;2023-02-21T10:00:00+01:00;6260
watt_hours_period;2023-02-21T11:00:00+01:00;7632
watt_hours_period;2023-02-21T12:00:00+01:00;7659
watt_hours_period;2023-02-21T13:00:00+01:00;7053
watt_hours_period;2023-02-21T14:00:00+01:00;5835
watt_hours_period;2023-02-21T15:00:00+01:00;4160
watt_hours_period;2023-02-21T16:00:00+01:00;2290
watt_hours_period;2023-02-21T17:00:00+01:00;820
watt_hours_period;2023-02-21T17:31:00+01:00;76
watt_hours_period;2023-02-22T06:49:00+01:00;0
watt_hours_period;2023-02-22T07:00:00+01:00;242
watt_hours_period;2023-02-22T08:00:00+01:00;3633
watt_hours_period;2023-02-22T09:00:00+01:00;5392
watt_hours_period;2023-02-22T10:00:00+01:00;6397
watt_hours_period;2023-02-22T11:00:00+01:00;6611
watt_hours_period;2023-02-22T12:00:00+01:00;6353
watt_hours_period;2023-02-22T13:00:00+01:00;5630
watt_hours_period;2023-02-22T14:00:00+01:00;4274
watt_hours_period;2023-02-22T15:00:00+01:00;2614
watt_hours_period;2023-02-22T16:00:00+01:00;1269
watt_hours_period;2023-02-22T17:00:00+01:00;455
watt_hours_period;2023-02-22T17:33:00+01:00;52
watt_hours;2023-02-21T06:50:00+01:00;0
watt_hours;2023-02-21T07:00:00+01:00;161
watt_hours;2023-02-21T08:00:00+01:00;2468
watt_hours;2023-02-21T09:00:00+01:00;6338
watt_hours;2023-02-21T10:00:00+01:00;12598
watt_hours;2023-02-21T11:00:00+01:00;20230
watt_hours;2023-02-21T12:00:00+01:00;27889
watt_hours;2023-02-21T13:00:00+01:00;34942
watt_hours;2023-02-21T14:00:00+01:00;40777
watt_hours;2023-02-21T15:00:00+01:00;44937
watt_hours;2023-02-21T16:00:00+01:00;47227
watt_hours;2023-02-21T17:00:00+01:00;48047
watt_hours;2023-02-21T17:31:00+01:00;48123
watt_hours;2023-02-22T06:49:00+01:00;0
watt_hours;2023-02-22T07:00:00+01:00;242
watt_hours;2023-02-22T08:00:00+01:00;3875
watt_hours;2023-02-22T09:00:00+01:00;9267
watt_hours;2023-02-22T10:00:00+01:00;15664
watt_hours;2023-02-22T11:00:00+01:00;22275
watt_hours;2023-02-22T12:00:00+01:00;28628
watt_hours;2023-02-22T13:00:00+01:00;34258
watt_hours;2023-02-22T14:00:00+01:00;38532
watt_hours;2023-02-22T15:00:00+01:00;41146
watt_hours;2023-02-22T16:00:00+01:00;42415
watt_hours;2023-02-22T17:00:00+01:00;42870
watt_hours;2023-02-22T17:33:00+01:00;42922
watt_hours_day;2023-02-21;48123
watt_hours_day;2023-02-22;42922
JQ Template (called with --raw-data flag):
{ south: [inputs | split(";") | {label: .[0], date: .[1], values: .[2] | gsub("[\r]"; "") | tonumber } ]
| group_by(.label) | map({ key: (.[0].label), value: [.[] | {date: .date, values: .values}] }) | from_entries
}
If I examine the results, the first entry is missing. I do not understand why. It is not the group_by section or anything because if I remove that it also fails to grab the first line. And it is not in the array later or anything, it is just missing.
答案1
得分: 1
添加-n
或--null-input
选项。(在 jqplay.org 上勾选复选框,标记为“Null Input”)。
这是因为第一个输入被上下文.
消耗掉了,任何随后的输入都被通过重新迭代到.
(这在程序上自动发生)或者如果存在的话,被input
(一个)或inputs
(全部)提前消耗掉。要将第一个输入包括在通过input
或inputs
消耗的范围内,你必须在第一个输入前面“添加”null
(然后被.
消耗掉),这样所有其他输入(从第一个“真实”的输入开始)就留给了input
和inputs
来消耗。
英文:
Add the -n
or --null-input
option. (On jqplay.org tick the checkbox reading "Null Input").
This is because the first input is consumed by the context .
, and any subsequent inputs are consumed either by reiteration to .
(which happens programmatically), or earlier, if present, by the use of input
(one) or inputs
(all). The include the first input into the consumption via input
or inputs
you have to sort of prepend null
as a new first input (which is then consumed by .
), so all the other ones (beginning with the first "real" input) are left to be consumed by input
and inputs
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论