gpt4 book ai didi

python - 使用Python修改csv中的特定列

转载 作者:行者123 更新时间:2023-11-28 16:48:04 25 4
gpt4 key购买 nike

我在 .csv 中有一堆数据,如下所示:

-959.378170,-0.000026,-94.960000,1508.000000,9.000000,
-958.978170,-0.000026,-94.920000,1508.000000,9.000000,
-958.578170,-0.000026,-94.880000,1508.000000,10.000000,
-958.178170,-0.000026,-94.840000,1508.000000,10.000000,
-957.778170,-0.000026,-94.800000,1508.000000,10.000000,

最后两列应该是时间。 15 是小时,08 是分钟,6 是秒。最终目标是加入他们,这样我就能得到类似的东西:

-958.978170,-0.000026,-94.920000,15:08:09,                
-958.578170,-0.000026,-94.880000,15:08:10,

我该怎么做?

最佳答案

我会使用 regexfileinput

import fileinput
import re

# Assume the input file is foo.csv
for line in fileinput.FileInput('foo.csv', inplace=1):
mm = re.search(r'^(.+?,.+?,.+?,)(\d{1,2})(\d{2})\.0+,(\d{1,2})\.0+',
line)
g1, g2, g3, g4 = mm.group(1), int(mm.group(2)), int(mm.group(3)), int(mm.group(4))
print "%s%02i:%02i:%02i," % (g1, g2, g3, g4)

在示例中运行此命令会导致...

-959.378170,-0.000026,-94.960000,15:08:09,
-958.978170,-0.000026,-94.920000,15:08:09,
-958.578170,-0.000026,-94.880000,15:08:10,
-958.178170,-0.000026,-94.840000,15:08:10,
-957.778170,-0.000026,-94.800000,15:08:10,

关于python - 使用Python修改csv中的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11750173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com