Parsing a text file into a pandas DataFrame I have a .txt file that looks like this:
SHT1 E:   T1:30.45°C    H1:59.14 %RH
SHT2 S:   T2:29.93°C    H2:67.38 %RH

SHT1 E:   T1:30.49°C    H1:58.87 %RH
SHT2 S:   T2:29.94°C    H2:67.22 %RH

SHT1 E:   T1:30.53°C    H1:58.69 %RH
SHT2 S:   T2:29.95°C    H2:67.22 %RH


I want to have a DataFrame that looks like this:
      T1     H1     T2     H2
0  30.45  59.14  29.93  67.38
1  30.49  58.87  29.94  67.22
2  30.53  58.69  29.95  67.22

I parse this by:

Reading up the text file line by line
Parsing the lines e.g. matching only the parts with T1, T2, H1, and H2, splitting by :, and removing °C and %RH
The above produces a list of lists each having two items
I flatten the list of lists
Just to chop it up into a list of four-item lists
Dump that to a df
Write to an Excel file

Here's the code:
import itertools

import pandas as pd


def read_lines(file_object) -&gt; list:
    return [
        parse_line(line) for line in file_object.readlines() if line.strip()
    ]


def parse_line(line: str) -&gt; list:
    return [
        i.split(&quot;:&quot;)[-1].replace(&quot;°C&quot;, &quot;&quot;).replace(&quot;%RH&quot;, &quot;&quot;)
        for i in line.strip().split()
        if i.startswith((&quot;T1&quot;, &quot;T2&quot;, &quot;H1&quot;, &quot;H2&quot;))
    ]


def flatten(parsed_lines: list) -&gt; list:
    return list(itertools.chain.from_iterable(parsed_lines))


def cut_into_pieces(flattened_lines: list, piece_size: int = 4) -&gt; list:
    return [
        flattened_lines[i:i + piece_size] for i
        in range(0, len(flattened_lines), piece_size)
    ]


with open(&quot;your_text_data.txt&quot;) as data:
    df = pd.DataFrame(
        cut_into_pieces(flatten(read_lines(data))),
        columns=[&quot;T1&quot;, &quot;H1&quot;, &quot;T2&quot;, &quot;H2&quot;],
    )
    print(df)
    df.to_excel(&quot;your_table.xlsx&quot;, index=False)

This works and I get what I want but I feel like points 3, 4, and 5 are a bit of redundant work, especially creating a list of list just to flatten it and then chop it up again.

Question:
How could I simplify the whole parsing process? Or maybe most of the heavy-lifting can be done with pandas alone?
Also, any other feedback is more than welcomed.
Disclaimer: I know this is a very liberal interpretation of a code review since it suggests an entirely different approach. I still thought it might provide a useful perspective when thinking about such problems in the future and reducing coding effort.
I would suggest the following approach using regex to extract all the numbers that match the format &quot;12.34&quot;.
import re
import pandas as pd

with open(&quot;your_text_data.txt&quot;) as data_file:
    data_list = re.findall(r&quot;\d\d\.\d\d&quot;, data_file.read())

result = [data_list[i:i + 4] for i in range(0, len(data_list), 4)]

df = pd.DataFrame(result, columns=[&quot;T1&quot;, &quot;H1&quot;, &quot;T2&quot;, &quot;H2&quot;])
print(df)
df.to_excel(&quot;your_table.xlsx&quot;, index=False)

This will of course only work for the current data format you provided. The code will need to be adjusted if the format of your data changes. For example: If relevant numbers may contain a varying number of digits, you might use the regex &quot;\d+\.\d+&quot; to match all numbers that contain at least one digit on either side of the decimal point.
Also please note the use of the context manager with open(...) as x:. Only code that accesses the object needs to and should be part of the managed context.