python-3.x 将水平堆叠的数据排列成垂直

mzsu5hc0  于 2023-04-13  发布在  Python
关注(0)|答案(1)|浏览(105)

我有下面的示例ACSII文件需要更正。数据是水平堆叠的,但我想垂直排列它们,这样我就有六列相应的数据。

x,y,z,radius,parent_id,section_id
1.11249,7.12604,0.115005,0.301656,-1,0, 1.09154,7.11252,2.84736,0.282394,0,94, 0.995677,7.1256,5.32645,0.253602,1,243, 
1.56843,14.3332,0.438004,0.323019,-1,1, 1.51095,14.3979,2.96253,0.305156,0,95, 1.42625,14.5654,5.3787,0.302941,1,244, 
0.90128,26.929,0.931,0.139174,-1,2, 0.838613,26.887,2.96452,0.117682,0,96, 0.893118,26.7723,4.87435,0.107016,1,245, 
1.07621,2.70333,-0.00799561,0.0188232,-1,3, 1.07818,2.69581,0.0687787,0.0185532,0,97, 1.06829,2.71064,0.147124,0.0180435,1,246, 
2.01933,14.1152,0.553009,0.027311,-1,4, 1.92055,14.0809,0.676664,0.0183139,0,98, 1.85329,14.0558,0.734481,0.0183139,1,247, 
2.47447,5.00877,0.122009,0.029722,-1,6, 2.45128,5.01272,0.231932,0.0242449,0,102, 2.4809,4.98035,0.352266,0.0242449,1,251, 
3.49363,6.49478,0.309006,0.17034,-1,7, 3.42342,6.41083,2.42648,0.17034,0,104, 3.4452,6.49571,4.59179,0.132553,1,253, 
2.89307,1.24796,0.101013,0.102582,-1,8, 2.89746,1.25605,1.72609,0.0968505,0,105, 2.92189,1.32089,3.274,0.083222,1,254, 
3.83418,5.92341,0.200012,0.105705,-1,10, 3.86419,6.02115,0.721058,0.0512554,0,106, 3.88681,6.00418,1.03348,0.0512554,1,255, 
3.24276,29.1196,1.05,0.0286015,-1,11, 3.60552,29.3127,1.16927,0.0279337,0,107, 3.70438,29.2982,1.26589,0.0271196,1,256, 
2.9572,29.7666,1.186,0.014582,-1,12, 2.96082,29.7584,1.32545,0.014582,0,108, 2.96823,29.7646,1.44569,0.014582,1,257,
iszxjhcz

iszxjhcz1#

通过在6列片段上分离每行:

with open('test.txt') as f:
    print(next(f).strip()) # print column names
    for line in f:
        cols = line.strip().rstrip(',').split(',')
        for i in range(0, len(cols), 6):
            print(','.join(map(str.strip, cols[i: i + 6])))
x,y,z,radius,parent_id,section_id
1.11249,7.12604,0.115005,0.301656,-1,0
1.09154,7.11252,2.84736,0.282394,0,94
0.995677,7.1256,5.32645,0.253602,1,243
1.56843,14.3332,0.438004,0.323019,-1,1
1.51095,14.3979,2.96253,0.305156,0,95
1.42625,14.5654,5.3787,0.302941,1,244
0.90128,26.929,0.931,0.139174,-1,2
0.838613,26.887,2.96452,0.117682,0,96
0.893118,26.7723,4.87435,0.107016,1,245
1.07621,2.70333,-0.00799561,0.0188232,-1,3
1.07818,2.69581,0.0687787,0.0185532,0,97
1.06829,2.71064,0.147124,0.0180435,1,246
2.01933,14.1152,0.553009,0.027311,-1,4
1.92055,14.0809,0.676664,0.0183139,0,98
1.85329,14.0558,0.734481,0.0183139,1,247
2.47447,5.00877,0.122009,0.029722,-1,6
2.45128,5.01272,0.231932,0.0242449,0,102
2.4809,4.98035,0.352266,0.0242449,1,251
3.49363,6.49478,0.309006,0.17034,-1,7
3.42342,6.41083,2.42648,0.17034,0,104
3.4452,6.49571,4.59179,0.132553,1,253
2.89307,1.24796,0.101013,0.102582,-1,8
2.89746,1.25605,1.72609,0.0968505,0,105
2.92189,1.32089,3.274,0.083222,1,254
3.83418,5.92341,0.200012,0.105705,-1,10
3.86419,6.02115,0.721058,0.0512554,0,106
3.88681,6.00418,1.03348,0.0512554,1,255
3.24276,29.1196,1.05,0.0286015,-1,11
3.60552,29.3127,1.16927,0.0279337,0,107
3.70438,29.2982,1.26589,0.0271196,1,256
2.9572,29.7666,1.186,0.014582,-1,12
2.96082,29.7584,1.32545,0.014582,0,108
2.96823,29.7646,1.44569,0.014582,1,257

相关问题