读取一个30g以上的csv文件,逐行读取该csv文件,文件有五列,分别为schedulerid、nameid、addlongtime、variableid、val。 当读取到行,第二列为47且第三列时间相同时,时间格式举例为20220223082350,构建新的csv文件,文件一共有三列,第一列为schedule,第二列为time,第三列为weiyi,将连续相同的第三列情况下的第五列数值,写入在新建csv文件weiyi下的一个单元格中每个数值以“,”隔开,且位移格式为0.22,0.15,0.14,0.07,0.07,0.0,并将第三列相同的时间改变格式为2022/02/23 08:23:50写入新建csv文件time中,第一列写入新建文件csv文件schedule中。
时间: 2023-06-02 10:02:13 浏览: 83
代码如下:
```python
import csv
import os
import datetime
# 读取原始csv文件
filename = 'original.csv'
with open(filename, 'r') as f:
reader = csv.reader(f)
header = next(reader) # 获取表头
data = [row for row in reader] # 获取数据
# 构建新的csv文件
new_header = ['schedule', 'time', 'weiyi']
new_data = []
prev_time = None
prev_weiyi = None
for row in data:
nameid = int(row[1])
addlongtime = row[2]
weiyi = float(row[4])
if nameid == 47 and addlongtime == prev_time:
if weiyi != prev_weiyi:
prev_weiyi = weiyi
new_data[-1][-1].append(weiyi)
else:
prev_time = addlongtime
prev_weiyi = weiyi
new_data.append([row[0], addlongtime, [weiyi]])
# 将时间格式改为YYYY/MM/DD HH:MM:SS
for row in new_data:
row[1] = datetime.datetime.strptime(row[1], '%Y%m%d%H%M%S').strftime('%Y/%m/%d %H:%M:%S')
# 将位移格式改为0.22,0.15,0.14,0.07,0.07,0.0
for row in new_data:
row[2] = ','.join(['{:.2f}'.format(w) for w in row[2]])
# 写入新的csv文件
new_filename = 'new.csv'
with open(new_filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(new_header)
writer.writerows(new_data)
```
阅读全文