I receive text files that are structured like this:
random
----new data-----
06/19/2018 13:57:39.99 random information here
06/19/2018 13:58:24.99 some more random info
06/19/2018 13:58:35.08 00:00:04.38 A 00000 0 765 228270257 A0 44 45
06/19/2018 13:58:39.99 00:00:00.00 A 00000 0 756 228270257 A0 4 5
06/19/2018 13:58:40.61 00:00:00.00 A 00000 0 828 228270257 A0 1 7
06/19/2018 13:57:39.99 random information here
06/19/2018 13:58:24.99 some more random info
---end data---
random stuff
There are several lines with random information surrounding the actual data I care about. I only want to keep the rows that have A in the fourth row, and then I want to turn the data into a CSV file.
Assuming the data above is in play.txt, I have tried several variants of this. which isn't working:
import csv
import pandas as pd
from io import StringIO
id = []
with open('play.txt', 'r') as fi:
for ln in fi:
if ln.startswith("A",4):
id.append(ln[0:])
id2 = ' '.join(id)
df = pd.read_table(StringIO(id2), delimiter=r'\s+', header=None)
print(df)
df.to_csv('out.csv')
How can this be done in python?