Search code examples
pythonpandasdataframeextractfile-read

With Python or Pandas, extract only the strings from a txt or dat-file


I have a .dat-file with the following shape and being a few hundred lines long:

AlOH                 200  6000  1000
 7.882068110E+05 -2.263671626E+03  7.823954880E+00  1.821171456E-04 -8.263729320E-08  1.265414876E-11 -6.875972530E-16 -1.039808093E+04 -2.209032458E+01
 5.876493180E+04 -9.449422690E+02  7.820599180E+00  5.858888470E-04 -4.083666810E-06  4.587229340E-09 -1.563936726E-12 -1.993283011E+04 -2.065043885E+01
Al2O                 200  6000  1000
-1.171074351E+05 -1.783009166E+02  7.633215360E+00 -5.335931770E-05  1.180702791E-08 -1.355444579E-12  6.287323890E-17 -1.947580149E+04 -1.415764167E+01
 7.776530700E+03 -1.294235361E+02  4.912509520E+00  8.604223450E-03 -1.217703648E-05  8.314634870E-09 -2.237722201E-12 -1.886512879E+04 -2.806368311E-02
Al2O3                200  6000  1000
-2.777784969E+05 -4.917465930E+02  1.386703888E+01 -1.469381940E-04  3.250406490E-08 -3.730867350E-12  1.730444284E-16 -6.790757850E+04 -4.375559873E+01
-7.443374320E+03  8.829004210E+01  5.264662640E+00  2.507678848E-02 -3.434541650E-05  2.302516980E-08 -6.122529280E-12 -6.872685950E+04  2.202324298E+00

I want to extract only the chemical names (so only the strings) from it, preferably into a list like [AlOH, Al2O, Al2O3, ...]. I tried to do this with pandas, but due to the strange format of the columns the file is not being properly read. I haven't found on the internet any other solution that would be short and simple, though this should have a nice pythonic solution.

Has any one a solution how to extract only the strings?


Suggested solution:

chemicals = []
with open('bla_file.dat') as file:
    for line in file: 
        line = line.split()
        for item in line:
            try:
                float(item)
            except ValueError:
                chemicals.append(item)

Please post any solutions that might be simpler or shorter!


Solution

  • You could use list comprehensions for creating the chemicals headers and regular expressions for matching its names:

    with open('bla_file.dat') as f:
    
        chemicals = [re.findall('^\w+',line)[0] for line in f.readlines() if re.search('^\w+',line)]
    

    On the example you showed it returns:

    ['AlOH', 'Al2O', 'Al2O3']

    In this example you are simply matching the pattern desired for the chemicals names on each line and adding it to a list if matched. But you still have to read line by line for creating the list.