首页 > 解决方案 > 如何从excel中拆分数据集(txt文件)

问题描述

给定文本文件中的以下数据,我尝试按如下方式运行以下代码,但收到错误,例如无法将字符串转换为浮点数。看来我的字符串没有正确拆分。可能是什么问题?

def main():
    with open('[filename].txt) as f:
    lines = f.readlines()
    
    for line in lines:
        line = line.strip()
        items = line.split()

        arbor = int(items[0])
        button_cav_dia = float(items[1])
        shaft_dia = float(items[2])
        pos_stop1 = float(items[3])
        pos_stop2 = float(items[4])
        pos_stop3 = float(items[5])
        probing_surf_off = float(items[6])

1 14.09 12.7 4.459 4.461 4.461 1.278

2 14.09 12.7 4.458 4.472 4.474 1.281

3 14.09 12.7 4.468 4.59 4.478 1.286

4 14.09 12.7 4.463 4.71 4.471 1.278

5 14.8 12.71 4.439 4.411 4.419 1.274

6 14.09 12.71 4.398 4.457 4.47 1.277

7 14.09 12.7 4.457 4.461 4.494 1.28

8 14.08 12.71 4.464 4.475 4.466 1.283

9 14.09 12.7 4.451 4.471 4.474 1.282

10 14.09 12.71 4.411 4.416 4.423 1.265

11 14.09 12.7 4.439 4.475 4.443 1.287

12 14.09 12.71 4.476 4.466 4.506 1.289

13 14.09 12.71 4.464 4.47 4.466 1.277

14 14.08 12.71 4.477 4.461 4.444 1.282

15 14.09 12.68 4.448 4.455 4.443 1.288

标签: splitstrip

解决方案


推荐阅读