You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JQ处理CSV原始数据时首行丢失的问题求助

问题

使用指定JQ模板(搭配--raw-data参数调用)处理数据时,输出丢失第一行内容。即使移除group_by相关逻辑,该行数据依然不会出现在结果中。

原始数据

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模板

{ 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
}
原因分析

问题出在inputs的使用逻辑上:当用{ south: [inputs | ...] }这种写法时,JQ会将第一行输入作为构建外层对象的上下文,inputs只能读取从第二行开始的剩余内容,直接导致第一行被跳过。

解决方案

要保留所有输入行,需先将所有输入收集为完整数组再处理。修正后的JQ模板如下:

[inputs]
| map(split(";") | {label: .[0], date: .[1], values: .[2] | gsub("[\r]"; "") | tonumber})
| group_by(.label)
| map({key: .[0].label, value: [.[] | {date: .date, values: .values}]})
| from_entries
| {south: .}

修正说明

  • [inputs]会将所有输入行(包括第一行)捕获为一个数组,避免第一行被作为上下文消耗。
  • 后续的map、group_by等操作基于完整数组执行,就能保留全部数据行。

内容的提问来源于stack exchange,提问作者Kevin Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 23:01:31