txt文件内容:
[141.09, 117.01]
[126.97, 159.7, 159.3]
[113.63, 152.15, 151.56, 144.62, 423.84]
将文件内容转为一行,并去掉[],最终结果展示:
[141.09, 117.01,126.97, 159.7, 159.3,113.63, 152.15, 151.56, 144.62, 423.84,142.02, 148.17, 96.45, 112.09]
代码如下:
list1=[]
file=open("../test.txt")
lines = file.readlines()
# print(lines)
pi_string = ''
for line in lines:
pi_string += line.rstrip()
# print(pi_string)
out=pi_string.replace("[","").replace("]",", ")
res=out.split(',')
print(res)
del res[-1]
print(res)
number = list(map(float, res))
print(number)
得到结果:
[141.09, 117.01, 126.97, 159.7]