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]