我的输入是
status,avgMeasuredTime,avgSpeed,extID,medianMeasuredTime,TIMESTAMP,vehicleCount,_id,REPORT_ID,Lat1,Long1,Lat2,Long2,Distance between 2 points,duration of measurements,ndt in kmh
OK,74,50,668,74,1406859600,5,20746220,158324,56.23172069428216,10.104986076057457,56.23172069428216,56.22579478256016,1030,52,71
OK,926,4,981,926,1412098500,0,28060227,210173,56.20913963031665,10.246642527612721,56.20913963031665,56.2026461982616,1106,88,45
预期产出:
status,avgMeasuredTime,avgSpeed,extID,medianMeasuredTime,TIMESTAMP,vehicleCount,_id,REPORT_ID,Lat1,Long1,Lat2,Long2,Distance between 2 points,duration of measurements,ndt in kmh
OK,74,50,668,74,1406859600,5,20746220,158324,56.2317,10.1050,56.2317,56.2258,1030,52,71
OK,926,4,981,926,1412098500,0,28060227,210173,56.2091,10.2466,56.2091,56.2026,1106,88,45
如你所见,我想要完成第10,11,12,13列。
请帮忙。提前谢谢。
答案 0 :(得分:2)
awk 方法:
awk 'BEGIN{FS=OFS=","}NR>1{for(i=NF-3;i>NF-7;i--) $i=sprintf("%.4f",$i)}1' file
输出:
status,avgMeasuredTime,avgSpeed,extID,medianMeasuredTime,TIMESTAMP,vehicleCount,_id,REPORT_ID,Lat1,Long1,Lat2,Long2,Distance between 2 points,duration of measurements,ndt in kmh
OK,74,50,668,74,1406859600,5,20746220,158324,56.2317,10.1050,56.2317,56.2258,1030,52,71
OK,926,4,981,926,1412098500,0,28060227,210173,56.2091,10.2466,56.2091,56.2026,1106,88,45
for(i=NF-3;i>NF-7;i--)
- 从最后的第4个字段开始迭代4个字段
注意 :如果列数始终是静态的,您可以通过其位置编号直接访问它们:
awk 'BEGIN{FS=OFS=","}NR>1{for(i=10;i<=13;i++) $i=sprintf("%.4f",$i)}1' file
如果您想要 Python 方法 - 请点击此处:
with open("yourfile", 'r') as f:
for k,l in enumerate(f.read().splitlines()):
if k > 0:
items = l.split(',')
items[-4:-8:-1] = ["%.4f" % float(i) for i in items[-4:-8:-1]]
l = ','.join(items)
print(l)
输出将是相同的