使用python循环文本文件中的行

瑞安

我收到结构如下的文本文件:

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

我关心的实际数据周围有几行随机信息。我只想保留A第四行中的行,然后我想将数据转换为 CSV 文件。

假设上面的数据在 中play.txt,我已经尝试了它的几种变体。这不起作用:

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')

这怎么能在python中完成?

登录
# read the file
file = open('play.txt').read()

id = []

# loop through the file and if the fourth word is 'A' then append that line to 'id'
for line in file.splitlines():
    if line.split()[3] == 'A':
        id.append(line.split())

# save to a dataframe
df = pd.DataFrame(id)
df
    0           1           2           3   4       5   6   7           8   9   10
0   06/19/2018  13:58:35.08 00:00:04.38 A   00000   0   765 228270257   A0  44  45
1   06/19/2018  13:58:39.99 00:00:00.00 A   00000   0   756 228270257   A0  4   5
2   06/19/2018  13:58:40.61 00:00:00.00 A   00000   0   828 228270257   A0  1   7

# if you want specify column names too 
# df = pd.DataFrame(id, columns=['col_name_1', 'col_name_2'... ])

# save to csv
df.to_csv('out.csv')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章