只是因为我觉得它会很有趣,所以我编写了一些代码。但是,您的测试数据与您解释的并不完全相符,尤其是前两条数据线具有相同的Tag,尚不清楚该怎么做。不过,这里是您可以根据需要破解的代码。希望对您有所帮助。
我们的想法是使用 DictReader 和 DictWriter 来管理具有列名的单元格,而不关心它们的读取/写入顺序,直到确实有必要。然后,我使用data 字典,它可以帮助我基于任意键将行合并在一起,该键由某些特定单元格的值定义,使用元组可以用作字典中的键这一事实。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
import sys
# This will store the lines by tag, in order to join them
data = {}
# This will tell us how to order the columns, will be extended later
columns = ['L', 'Dr', 'Tag']
# Controls extension of the columns
maxhour = 0
# Controls the order the keys are found in the original CSV. Maybe not necessary
keys = []
with open("in.csv", 'r', newline='') as fin:
reader = csv.DictReader(fin)
for row in reader:
hour = int(row['Hour'])
# Form a unique key to match lines. Adjust to your needs
key = (row['L'], row['Dr'], row['Tag'])
if key not in data:
# This is a future row, a dict with column as key, cell as value
data[key] = {'L': row['L'], 'Dr': row['Dr'], 'Tag': row['Tag']}
# Remember the order we've seen the keys
keys.append(key)
# Now, add data to the row for each minutes
# 1 to 59
for minute in range(1,60):
# Copy data from column 'minute' to column 'Hour:minute'
src_colname = str(minute)
dest_colname = row['Hour'] + ':' + src_colname
data[key][dest_colname] = row[src_colname]
# There seems to be a special treatment for minute 0, at column "L 0"
if hour == 0:
data[key]['L 0'] = row['L 0']
else:
data[key][row['Hour'] + ':0'] = row['L 0']
# Plan to generate enough columns when writing resulting file
maxhour = max(maxhour, hour)
with open("out.csv", 'w', newline='') as fout:
# Okay, now everything was merged into data
# We need to tell DictWriter how to order columns
# Treat special first column
columns.append('L 0')
# Then add the rest
for hour in range(0, maxhour+1):
# Do not include "0:0"
for minute in range(0 if hour > 0 else 1,60):
columns.append('{:d}:{:d}'.format(hour, minute))
# Let's write that
writer = csv.DictWriter(fout, columns, restval = "0")
writer.writeheader()
for key in keys: # or for key in data.keys(): if you don't mind the order
writer.writerow(data[key])
测试数据in.csv:
Hour,L,Dr,Tag,L 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59
0,L5,XI,PS,4R,6,3,6,6,5,6,1,9,11,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,L5,XI,PS,4R,5,8,10,7,7,8,3,9,5,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,L0,St,v2T,4R,1,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,L2,TI,sst,4R,8,8,8,8,8,8,8,8,8,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,L5,XI,PS,4R,8,8,8,8,8,8,8,8,8,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
然后输出:
L,Dr,Tag,L 0,0:1,0:2,0:3,0:4,0:5,0:6,0:7,0:8,0:9,0:10,0:11,0:12,0:13,0:14,0:15,0:16,0:17,0:18,0:19,0:20,0:21,0:22,0:23,0:24,0:25,0:26,0:27,0:28,0:29,0:30,0:31,0:32,0:33,0:34,0:35,0:36,0:37,0:38,0:39,0:40,0:41,0:42,0:43,0:44,0:45,0:46,0:47,0:48,0:49,0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