使用Python-3.x转置csv文件

时间:2019-07-19 18:47:07

标签: csv

我有一个非标准的csv文件,带有两组标题。我需要剥离前三行并将数据行转置为数据列。

我能够剥离前三行中的标题,但无法将行转置为列。

stream-start,fractional-seconds-start,decimation-ratio,lowpass_bandwidth_hz,rf-sampling_rate_sps,effective-sampling-rate,stream-samples,fft-frame-size
2019-07-08T22:35:41Z,0.39054,32,1e+07,1.6e+08,5e+06,16384,1024
elapsed-seconds,channel,center-frequency-hz,minimum-frequency-hz,channel-size-hz
39334,1,1419800000.000000,1417300000.000000,305.175781,-66.41,-66.11,-66.39,-66.37,-66.58,-66.25,-66.30,-66.33,-66.60,-66.15,-66.31,-66.27,-66.18,-66.16,-66.28,-66.39,-66.40,-66.38,-66.46,-66.42,-66.34,-66.43,-66.48,-66.20,-66.60,-66.53,-66.51,-66.19,-66.20,-66.36,-66.39,-66.41,-66.48,-66.57,-66.35,-66.65,-66.28,-66.27,-66.01,-66.46,-66.33,-66.43,-66.16,-66.65,-66.62,-66.26,-66.21,-66.47,-66.24,-66.69,-66.46,-66.49,-66.68,-66.51,-66.32,-66.28,-66.25,-66.09,-66.23,-66.60,-66.61,-66.67,-66.58,-66.38,-66.41,-66.22,-66.55,-66.54,-66.53,-66.34,-66.56,-66.56,-66.35,-66.71,-66.36,-66.40,-66.44,-66.18,-66.42,-66.49,-66.65,-66.37,-66.41,-66.38,-66.19,-66.52,-66.48,-65.93,-66.47,-66.52,-66.69,-66.63,-66.35,-66.29,-66.39,-66.31,-66.22,-66.50,-66.23,-66.32,-66.58,-66.54,-66.47,-66.52,-66.54,-66.41,-66.65,-66.51,-66.45,-66.35,-66.52,-66.62,-66.33,-66.25,-66.26,-66.56,-66.54,-66.40,-66.34,-66.24,-66.74,-66.27,-66.36,-66.47,-66.56,-66.45,-66.46,-66.67,-66.50,-66.62,-66.43,-66.48,-66.26,-66.48,-66.36,-66.14,-66.68,-66.21,-66.30,-66.27,-66.27,-66.42,-66.55,-66.30,-66.15,-66.40,-66.35,-66.48,-66.54,-66.11,-66.33,-66.46,-66.62,-66.21,-66.63,-66.75,-66.58,-66.54,-66.72,-66.34,-66.36,-66.46,-66.51,-66.39,-66.14,-66.32,-66.47,-66.43,-66.69,-66.75,-66.49,-66.42,-66.54,-66.57,-66.39,-66.49,-66.50,-66.54,-66.68,-66.39,-66.49,-66.53,-66.58,-66.25,-66.30,-66.11,-66.34,-66.22,-66.30,-66.39,-66.23,-66.29,-66.45,-66.48,-66.53,-66.44,-66.46,-66.37,-66.60,-66.33,-66.55,-66.54,-66.56,-66.19,-66.62,-66.35,-66.21,-66.22,-66.42,-66.17,-66.41,-66.12,-66.25,-66.38,-66.51,-66.44,-66.56,-66.53,-66.09,-66.27,-66.25,-66.28,-66.48,-66.45,-66.18,-66.50,-66.17,-66.31,-66.18,-66.29,-66.32,-66.41,-66.33,-66.52,-66.34,-66.72,-66.66,-66.38,-66.37,-66.68,-66.39,-66.28,-66.50,-66.42,-66.45,-66.27,-66.44,-66.43,-66.31,-66.38,-66.38,-66.26,-66.33,-66.20,-66.57,-66.43,-66.16,-66.39,-66.31,-66.51,-66.44,-66.43,-66.47,-66.38,-66.61,-66.25,-66.48,-66.22,-66.03,-66.66,-65.99,-66.61,-66.45,-66.55,-66.49,-66.18,-66.32,-66.32,-66.38,-66.27,-66.52,-66.23,-66.50,-66.35,-66.42,-66.50,-66.30,-66.22,-66.45,-66.13,-66.15,-66.43,-66.35,-66.41,-66.51,-66.60,-66.14,-66.51,-66.48,-66.22,-66.40,-66.36,-66.40,-66.56,-66.52,-66.62,-66.60,-66.27


file_in="small_fft.txt"
file_out="myfile.csv"

with open(file_in,'r') as reader:
    next(reader)
    next(reader)
    next(reader)
    file_contents=reader.readlines()

with open(file_out,'w') as writer:
    writer.writelines(file_contents)

with open('myfile.csv') as f, open('destination.csv', 'w') as fw: 
    writer(fw, delimiter=',').writerows(zip(*reader(f, delimiter=',')))

所需的输出csv(destination.csv)将第4行到N从行转换为列。

0 个答案:

没有答案