我有一个文件夹,里面装满了以下格式的.txt文件(例如下面的0.txt(:
[[[ 2.1032608e+01 3.2608695e+00 1.4498953e-02]
[ 2.1032608e+01 3.0978260e+00 6.5927312e-04]
[ 3.0652174e+01 5.6739132e+01 1.1040760e-04]
[ 3.0489130e+01 5.6576088e+01 6.9526240e-04]
[ 1.6141304e+01 2.1684782e+01 1.5140385e-03]
[ 1.6141304e+01 2.1684782e+01 2.4998910e-03]
[ 3.5706520e+01 1.5163043e+01 1.5667630e-02]
[ 3.5706520e+01 1.5163043e+01 5.1132514e-04]
[ 1.6141304e+01 2.1521740e+01 9.0042617e-05]
[ 1.5652174e+01 2.0217392e+01 4.7678968e-05]
[ 3.3097828e+01 5.8043480e+01 2.3867608e-04]
[ 1.5652174e+01 2.0217392e+01 1.3099676e-04]
[ 1.7445652e+01 3.3097828e+01 8.3532266e-04]
[ 1.8423914e+01 2.5597826e+01 1.5159410e-02]
[ 3.8967392e+01 5.9673912e+01 6.5958439e-03]
[ 3.8967392e+01 5.9673912e+01 9.2201140e-03]
[ 1.5652174e+01 2.0054348e+01 9.5022479e-03]
[ 3.3097828e+01 3.9782608e+01 1.6697637e-04]
[ 3.3260868e+01 3.9619564e+01 2.7213297e-03]
[ 3.3260868e+01 3.9619564e+01 2.1958089e-04]
[ 1.6141304e+01 2.1684782e+01 5.6157220e-05]
[ 2.1032608e+01 3.0978260e+00 -1.0377635e-05]
[ 2.1032608e+01 3.0978260e+00 -8.7080858e-05]
[ 1.5652174e+01 2.0217392e+01 -1.1274265e-04]
[ 1.5652174e+01 2.0054348e+01 -5.1815634e-05]
[ 3.2119564e+01 5.9673912e+01 -1.6645924e-04]
[ 1.5652174e+01 2.0217392e+01 7.7641735e-06]
[ 3.2608697e-01 1.7934783e+00 3.5882247e-05]
[ 2.1032608e+01 3.0978260e+00 7.8685611e-05]
[ 1.5652174e+01 2.0054348e+01 3.1178934e-05]
[ 3.8967392e+01 5.9673912e+01 1.0482615e-04]
[ 3.1956522e+01 5.6739132e+01 1.0767143e-03]
[ 3.1793478e+01 5.6739132e+01 6.9617934e-04]
[ 3.1956522e+01 5.6739132e+01 1.2175621e-04]
[ 3.2119564e+01 5.9673912e+01 8.6526132e-05]
[ 3.8967392e+01 5.9673912e+01 7.2365336e-05]
[ 3.5706520e+01 1.5163043e+01 1.3151739e-04]
[ 2.1032608e+01 3.0978260e+00 4.4964137e-05]
[ 2.1032608e+01 3.0978260e+00 -1.4677797e-05]
[ 3.5706520e+01 1.5163043e+01 -1.1417971e-05]
[ 2.1032608e+01 3.0978260e+00 1.2694198e-05]
[ 2.1032608e+01 3.0978260e+00 8.1792423e-05]
[ 3.2119564e+01 5.9673912e+01 -4.8189460e-05]
[ 1.5652174e+01 2.0217392e+01 -1.2792133e-04]
[ 3.2445652e+01 5.9673912e+01 -1.1050291e-04]
[ 1.5652174e+01 2.0217392e+01 -4.2534582e-05]
[ 3.2282608e+01 5.9673912e+01 -3.9592214e-05]
[ 3.2445652e+01 5.9673912e+01 -1.2300081e-04]
[ 3.2119564e+01 5.9673912e+01 1.3895029e-04]
[ 3.2119564e+01 5.9673912e+01 4.5048160e-04]
[ 3.2119564e+01 5.9673912e+01 2.8149947e-03]
[ 3.2119564e+01 5.9673912e+01 2.4660570e-03]
[ 3.2119564e+01 5.9673912e+01 1.1084687e-05]
[ 1.8423914e+01 2.5597826e+01 2.7254851e-05]
[ 1.8423914e+01 2.5597826e+01 4.5520133e-06]
[ 3.5706520e+01 1.5163043e+01 1.3295857e-05]
[ 3.2282608e+01 5.9673912e+01 4.1052594e-06]
[ 3.2119564e+01 5.9673912e+01 5.6243367e-05]
[ 3.3097828e+01 5.8206520e+01 2.3062130e-05]
[ 3.2119564e+01 5.9673912e+01 6.7559289e-05]
[ 3.2119564e+01 5.9673912e+01 6.5806766e-05]
[ 3.2119564e+01 5.9673912e+01 9.2756242e-04]
[ 3.2119564e+01 5.9673912e+01 5.2335125e-04]
[ 3.2282608e+01 5.9673912e+01 3.2513806e-05]
[ 1.8423914e+01 2.5597826e+01 1.3968185e-05]
[ 3.2119564e+01 5.9673912e+01 1.5303758e-05]
[ 1.6141304e+01 2.1684782e+01 1.7933089e-05]
[ 3.2119564e+01 5.9673912e+01 6.1444991e-04]
[ 2.1032608e+01 3.0978260e+00 2.0746856e-04]
[ 1.5652174e+01 2.0054348e+01 -9.7850330e-05]]]
有什么方法可以将其读取到2d numpy数组中吗?请注意,它是由两个空格分隔的,但当有负数时,-号会代替其中一个空格。如有任何帮助,我们将不胜感激!
我试着把它读成panda,并以各种方式进行解析(使用df.str去掉括号等(,但我似乎无法让它以科学符号的方式工作。我在这里尝试了regex方法。
使用:
import re
with open('01.txt') as file:
p = file.read()
rows = p.split('n')
ll = []
for row in rows:
ll.append(re.findall('[d+-.e]+', row))
np.array(ll, dtype=float)
输出:
array([[ 2.1032608e+01, 3.2608695e+00, 1.4498953e-02],
[ 2.1032608e+01, 3.0978260e+00, 6.5927312e-04],
[ 3.0652174e+01, 5.6739132e+01, 1.1040760e-04],
[ 3.0489130e+01, 5.6576088e+01, 6.9526240e-04],
[ 1.6141304e+01, 2.1684782e+01, 1.5140385e-03],
[ 1.6141304e+01, 2.1684782e+01, 2.4998910e-03],
[ 3.5706520e+01, 1.5163043e+01, 1.5667630e-02],
[ 3.5706520e+01, 1.5163043e+01, 5.1132514e-04],
[ 1.6141304e+01, 2.1521740e+01, 9.0042617e-05],
[ 1.5652174e+01, 2.0217392e+01, 4.7678968e-05],
[ 3.3097828e+01, 5.8043480e+01, 2.3867608e-04],
[ 1.5652174e+01, 2.0217392e+01, 1.3099676e-04],
[ 1.7445652e+01, 3.3097828e+01, 8.3532266e-04],
[ 1.8423914e+01, 2.5597826e+01, 1.5159410e-02],
[ 3.8967392e+01, 5.9673912e+01, 6.5958439e-03],
[ 3.8967392e+01, 5.9673912e+01, 9.2201140e-03],
[ 1.5652174e+01, 2.0054348e+01, 9.5022479e-03],
[ 3.3097828e+01, 3.9782608e+01, 1.6697637e-04],
[ 3.3260868e+01, 3.9619564e+01, 2.7213297e-03],
[ 3.3260868e+01, 3.9619564e+01, 2.1958089e-04],
[ 1.6141304e+01, 2.1684782e+01, 5.6157220e-05],
[ 2.1032608e+01, 3.0978260e+00, -1.0377635e-05],
[ 2.1032608e+01, 3.0978260e+00, -8.7080858e-05],
[ 1.5652174e+01, 2.0217392e+01, -1.1274265e-04],
[ 1.5652174e+01, 2.0054348e+01, -5.1815634e-05],
[ 3.2119564e+01, 5.9673912e+01, -1.6645924e-04],
[ 1.5652174e+01, 2.0217392e+01, 7.7641735e-06],
[ 3.2608697e-01, 1.7934783e+00, 3.5882247e-05],
[ 2.1032608e+01, 3.0978260e+00, 7.8685611e-05],
[ 1.5652174e+01, 2.0054348e+01, 3.1178934e-05],
[ 3.8967392e+01, 5.9673912e+01, 1.0482615e-04],
[ 3.1956522e+01, 5.6739132e+01, 1.0767143e-03],
[ 3.1793478e+01, 5.6739132e+01, 6.9617934e-04],
[ 3.1956522e+01, 5.6739132e+01, 1.2175621e-04],
[ 3.2119564e+01, 5.9673912e+01, 8.6526132e-05],
[ 3.8967392e+01, 5.9673912e+01, 7.2365336e-05],
[ 3.5706520e+01, 1.5163043e+01, 1.3151739e-04],
[ 2.1032608e+01, 3.0978260e+00, 4.4964137e-05],
[ 2.1032608e+01, 3.0978260e+00, -1.4677797e-05],
[ 3.5706520e+01, 1.5163043e+01, -1.1417971e-05],
[ 2.1032608e+01, 3.0978260e+00, 1.2694198e-05],
[ 2.1032608e+01, 3.0978260e+00, 8.1792423e-05],
[ 3.2119564e+01, 5.9673912e+01, -4.8189460e-05],
[ 1.5652174e+01, 2.0217392e+01, -1.2792133e-04],
[ 3.2445652e+01, 5.9673912e+01, -1.1050291e-04],
[ 1.5652174e+01, 2.0217392e+01, -4.2534582e-05],
[ 3.2282608e+01, 5.9673912e+01, -3.9592214e-05],
[ 3.2445652e+01, 5.9673912e+01, -1.2300081e-04],
[ 3.2119564e+01, 5.9673912e+01, 1.3895029e-04],
[ 3.2119564e+01, 5.9673912e+01, 4.5048160e-04],
[ 3.2119564e+01, 5.9673912e+01, 2.8149947e-03],
[ 3.2119564e+01, 5.9673912e+01, 2.4660570e-03],
[ 3.2119564e+01, 5.9673912e+01, 1.1084687e-05],
[ 1.8423914e+01, 2.5597826e+01, 2.7254851e-05],
[ 1.8423914e+01, 2.5597826e+01, 4.5520133e-06],
[ 3.5706520e+01, 1.5163043e+01, 1.3295857e-05],
[ 3.2282608e+01, 5.9673912e+01, 4.1052594e-06],
[ 3.2119564e+01, 5.9673912e+01, 5.6243367e-05],
[ 3.3097828e+01, 5.8206520e+01, 2.3062130e-05],
[ 3.2119564e+01, 5.9673912e+01, 6.7559289e-05],
[ 3.2119564e+01, 5.9673912e+01, 6.5806766e-05],
[ 3.2119564e+01, 5.9673912e+01, 9.2756242e-04],
[ 3.2119564e+01, 5.9673912e+01, 5.2335125e-04],
[ 3.2282608e+01, 5.9673912e+01, 3.2513806e-05],
[ 1.8423914e+01, 2.5597826e+01, 1.3968185e-05],
[ 3.2119564e+01, 5.9673912e+01, 1.5303758e-05],
[ 1.6141304e+01, 2.1684782e+01, 1.7933089e-05],
[ 3.2119564e+01, 5.9673912e+01, 6.1444991e-04],
[ 2.1032608e+01, 3.0978260e+00, 2.0746856e-04],
[ 1.5652174e+01, 2.0054348e+01, -9.7850330e-05]])
好吧,正如我所理解的,你想要一个2d阵列
所以阶段是:
(伪python(
data=get string from "0.txt" file.read()..etc
line_splitted=data.split('n') # split by lines
2d_array=[]
for line in line_splitted:
# this is the method: * space split *the 'replace's are to remove the brackets
2d_array.append( line.split(' ').replace("[","").replace("]","") )
return 2d_array # you may convert it to numpy now, its a list of lists (2d list) ,
# /! each element is in string form, gotta be converted to float as well