Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active February 27, 2019 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/26aeb6d614c8adde95aff3719d5c4119 to your computer and use it in GitHub Desktop.
Save miraculixx/26aeb6d614c8adde95aff3719d5c4119 to your computer and use it in GitHub Desktop.
Read arbitrary formatted file into pandas dataframe
import pandas as pd
text = """
Men super men size Energy (J) type num g
----------------------------------------------------------------------
50 1 1 1.0234E+03 A abcd 12.1
20 7 4 5.0211E+02 A2 C agcd 14.1
10 2 3 -1.0347E+02 B2 abkd 72.1
"""
columns = []
data = []
import re
def read_text(fin):
for i, line in enumerate(fin):
line = line.split('\n')[0]
if i == 0:
columns = [val for val in re.split(r'\s{3}', line) if val]
continue
if i == 1:
continue
values = [val for val in re.split(r'\s{3}', line) if val]
row = {
col: val
for col, val in zip(columns, values)}
data.append(row)
return pd.DataFrame(data, columns=columns)
read_text(StringIO(text.strip()))
@miraculixx
Copy link
Author

Result

i Men super men size Energy (J) type num g
0 50 1 1 1.0234E+03 A abcd 12.1
1 20 7 4 5.0211E+02 A2 C agcd 14.1
2 10 2 3 -1.0347E+02 B2 abkd 72.1

@miraculixx
Copy link
Author

miraculixx commented Feb 27, 2019

Here's a two-liner that does the same thing

rows = [[val for val in re.split("\s{3}", line) if val and not val.startswith('---')] for line in text if line]
pd.DataFrame((row for row in rows[1:] if row), columns=rows[0])

Note the input has to be an array of lines, e.g.

text = """Men      super men        size       Energy (J)    type    num      g
----------------------------------------------------------------------
50          1             1          1.0234E+03    A      abcd   12.1
20          7             4          5.0211E+02    A2 C   agcd   14.1
10          2             3         -1.0347E+02    B2     abkd   72.1
""".split('\n')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment