I have a folder full of .txt files with the following format (one example, 0.txt below):
[[[ 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]]]
Is there any way to read this into a 2d numpy array? Notice that it is space delimited by two spaces, but when there is a negative number, the - sign is in place of one of the spaces. Any help would be greatly appreciated!
I tried reading it into pandas and parsing in various ways (using df.str to strip out brackets etc), but I couldn't seem to get it working in a way which reads in the scientific notation. I tried a regex approach here.
CodePudding user response:
Use:
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)
Output:
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]])
CodePudding user response:
Well , as I understood it, you want a 2d array
so the stages are:
(pseudo - 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