Python - Transpose Columns To Rows Within Data Operation And Before Writing To File
I have developed a public and open source App for Splunk (Nmon performance monitor for Unix and Linux Systems, see https://apps.splunk.com/app/1753/) A master piece of the App is a
Solution 1:
The goal is to transpose the data such as we will have in the header "time,device,value"
This rough transposition logic looks like this:
text = '''time,sda,sda1,sda2,sda3,sda5,sda6,sda7,sdb,sdb1,sdc,sdc1,sdc2,sdc3
26-JUL-2014 11:10:44,4.4,0.0,0.0,0.0,0.4,1.9,2.5,0.0,0.0,10.2,10.2,0.0,0.0
26-JUL-2014 11:10:54,4.8,0.0,0.0,0.0,0.3,2.0,2.6,0.0,0.0,5.4,5.4,0.0,0.0
26-JUL-2014 11:11:04,4.8,0.0,0.0,0.0,0.4,2.3,2.1,0.0,0.0,17.8,17.8,0.0,0.0
26-JUL-2014 11:11:14,2.1,0.0,0.0,0.0,0.2,0.5,1.5,0.0,0.0,28.2,28.2,0.0,0.0
'''import csv
for d in csv.DictReader(text.splitlines()):
time = d.pop('time')
for device, value insorted(d.items()):
print time, device, value
Putting it all together into a complete script looks something like this:
import csv
withopen('transposed.csv', 'wb') as destfile:
writer = csv.writer(destfile)
writer.writerow(['time', 'device', 'value'])
withopen('data.csv', 'rb') as sourefile:
for d in csv.DictReader(sourcefile):
time = d.pop('time')
for device, value insorted(d.items()):
row = [time, device, value]
writer.writerow(row)
Post a Comment for "Python - Transpose Columns To Rows Within Data Operation And Before Writing To File"