python如何按要求读取.txt文件中的数据

-14.0619 510 20.1439 500 53.7171 490 87.2903 480 121.823 470 155.396 460 189.928 450 223.501 440 257.075 430 291.607 420 325.18 410 359.713 400 393.189 390 426.763 380 461.295 370 494.868 360 529.401 350 562.974 340 596.547 330 631.079 320 664.652 310 699.185 300 732.758 290
499.186 590 507.726 580 516.924 570 525.469 560 534.02 550 543.236 540 551.803 530 560.38 520 568.969 510 578.235 500 586.854 490 595.489 480 604.809 470 613.484 460 622.851 450 631.574 440 640.995 430 649.771 420 659.253 410 668.771 400 677.643 390 687.234 380 696.866 370 706.541 360 716.26 350 725.569 340 735.363 330 745.184 320 755.026 310 764.884 300 774.75 290
1409.63 590 1389.99 580 1370.34 570 1350.7 560 1330.17 550 1310.52 540 1290.88 530 1271.24 520 1251.6 510 1231.06 500 1211.42 490 1191.78 480 1172.13 470 1152.49 460 1131.95 450 1112.31 440 1093.06 430 1072.53 420 1052.88 410 1033.24 400 1013.6 390 993.955 380 973.42 370 953.778 360 934.135 350 914.492 340 894.85 330 874.315 320 854.672 310 835.03 300 815.387 290
1650.42 440 1599.69 430 1549.69 420 1499.74 410 1449.74 400 1400.72 390 1350.76 380 1299.76 370 1249.78 360 1198.33 350 1146.86 340 1093.79 330 1038.28 320 982.064 310 922.834 300 862.567 290
如上是四条线的点对,怎么把每条线写成第一条线:[[-14.0619,510],[20.1439,500],...,(732.758,290)]第二条线:[[499.186,590],[507.726,580],...,[0 774.75,290]]

3个回答

没办法
你存的时候就应该存成EXCL或者数据库格式的
我没招

for line in data:
list = []
s = line.split()
num = len(s)
n = num//2
for i in range(1,n+1):
point = [s[2i-1],s[2i+1]]
list.append(point)
print(list)

#思路大概这样,细节你自己看看有没有错

两种方法,一种是numpy的数组,一种就两个for循环

import numpy
a = numpy.loadtxt('File.txt)'
with open('File.txt') as m:
    data = [line.rstrip('\n').split() for line in m]
    num_len = len(data)
    num_wid = len(data[0])

    for i in range(num_len):
        for j in range(num_wid):
            data[i][j] = int(data[i][j])
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问