我有以下示例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, 

推荐答案

通过用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

Python-3.x相关问答推荐

是否有必要使用Threads()中的args显式地将共享变量传递给Python中的线程函数或直接访问它?

使用递归将int转换为字符串

当索引大于一个整数而小于前一个索引时,我如何返回列值?

如何计算累积几何平均数?

匹配语句NaN

DataFrame列中如何迭代重复值?

pytorch 中 mps 设备的 manual_seed

使用 RANSAC 在激光雷达点云中查找电力线

如何将虚拟变量列转换为多列?

正则表达式从文本文件中捕获包含制表符/空格和子字符串的部分字符串

是否将dict转换为一个数据帧,每个值都有重复的键?

没有可重定向到的 URL.提供一个 url 或在模型上定义一个 get_absolute_url 方法

ValueError:找不到子字符串,我做错了什么?

Python 3 变量名中接受哪些 Unicode 符号?

是否有与 Laravel 4 等效的 python?

AttributeError:LinearRegression 对象没有属性coef_

如何将二进制(字符串)转换为浮点值?

Selenium Python - 处理没有这样的元素异常

在动态链接库 Anaconda3\Library\bin\mkl_intel_thread.dll 中找不到序数 242

Python:如何在 Windows 资源管理器中打开文件夹(Python 3.6.2、Windows 10)