Python 通过堆叠到其他列来重塑.csv数据

Python 通过堆叠到其他列来重塑.csv数据,python,database,csv,Python,Database,Csv,我有这样的原始数据 cp -0.094391 -0.26169 -0.33073 -0.22134 -0.086641 -0.022418 0.026102 0.15488 0.31659 0.47564 0.62409 0.69289 0.66066 0.39925 -0.098799 -0.26515 -0.33076 -0.22232 -0.086641 -0.021011 0.02751 0.16129 0.31832 0.47096 0.61332 . . . 我

我有这样的原始数据

cp
-0.094391
-0.26169
-0.33073
-0.22134
-0.086641
-0.022418
0.026102
0.15488
0.31659
0.47564
0.62409
0.69289
0.66066
0.39925
-0.098799
-0.26515
-0.33076
-0.22232
-0.086641
-0.021011
0.02751
0.16129
0.31832
0.47096
0.61332
   .
   .
   .

我想通过将四个值放入下一列来叠加所有值,如下所示。(我还想删除“cp”字母。)


如何使用
import csv
python模块重塑此数据?

这应该可以解决问题。将文件路径替换为您的路径以使其正常工作

import csv

final_csv = [[] for i in range(4)]

with open("./test.csv") as myfile:
    rd = csv.reader(myfile)
    no_header_rd = list(rd)[1:]
    i = 0
    for row in no_header_rd:
        final_csv[i % 4].append(row[0])
        i += 1

with open("./output.csv", 'w') as myoutputfile:
    wr = csv.writer(myoutputfile)
    wr.writerows(final_csv)
import csv

final_csv = [[] for i in range(4)]

with open("./test.csv") as myfile:
    rd = csv.reader(myfile)
    no_header_rd = list(rd)[1:]
    i = 0
    for row in no_header_rd:
        final_csv[i % 4].append(row[0])
        i += 1

with open("./output.csv", 'w') as myoutputfile:
    wr = csv.writer(myoutputfile)
    wr.writerows(final_csv)