【问题标题】:Python to read a File with start and stop conditionPython读取具有开始和停止条件的文件
【发布时间】:2019-12-12 03:51:39
【问题描述】:

您好,我有一个下面的文件数据,我希望对其进行处理以获得预期的输出,只是想知道作为 python 学习者是否有办法基于启动和停止布尔索引来实现这一点。

这里的文件行总是以名为 SRV: 的字符串开头,尽管在某些情况下这些行在同一行开始和结束,而在某些情况下这些行会扩展为换行符。

文件文本数据:

SRV: this is for bryan

SRV: this is for terry

SRV: this is for torain
sec01: This is reserved
sec02: This is open for all
sec03: Closed!

SRV: this is for Jun

预期输出:

SRV: this is for bryan

SRV: this is for terry

SRV: this is for torain sec01: This is reserved sec02: This is open for all sec03: Closed!

SRV: this is for Jun

有没有一种 Pythonic 方式可以更好地实现这一点,我也可以使用 pandas。

【问题讨论】:

  • 您可以尝试使用类似 df[0].groupby(df[0].str.startswith('SRV').cumsum()).apply(' '.join) 的内容,其中 0 是列名。 (注意:这是使用熊猫数据框)
  • @anky_91,这也可以。

标签: regex linux python-3.x pandas


【解决方案1】:

Series.str.startswithSeries.cumsum 用于组,然后通过GroupBy.aggjoin 聚合:

df1 = (df['col'].groupby(df['col'].str.startswith('SRV').cumsum())
                .agg(' '.join)
                .reset_index(drop=True)
                .to_frame(name='new'))
print (df1)
                                                 new
0                             SRV: this is for bryan
1                             SRV: this is for terry
2  SRV: this is for torain sec01: This is reserve...
3                               SRV: this is for Jun

详情

print (df['col'].str.startswith('SRV').cumsum())
0    1
1    2
2    3
3    3
4    3
5    3
6    4
Name: col, dtype: int32

对于DataFrame 使用:

import pandas as pd

temp=u"""col
SRV: this is for bryan

SRV: this is for terry

SRV: this is for torain
sec01: This is reserved
sec02: This is open for all
sec03: Closed!

SRV: this is for Jun"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), sep="|")

print (df)
                           col
0       SRV: this is for bryan
1       SRV: this is for terry
2      SRV: this is for torain
3      sec01: This is reserved
4  sec02: This is open for all
5               sec03: Closed!
6         SRV: this is for Jun

纯python解决方案:

out = []
with open("file.csv") as f1:
        last = 0
        for i, line in enumerate(f1.readlines()):
            if line.strip().startswith('SRV'):
                last = i
            out.append([line.strip(), last])

from itertools import groupby
from operator import itemgetter

with open("out_file.csv", "w") as f2:
    groups = groupby(out, key=itemgetter(1))
    for _, g in groups:
        gg = list(g)
        h = ' '.join(list(map(itemgetter(0), gg)))
        f2.write('\n' + h)

【讨论】:

  • 这确实很棒@-jezrael +1
  • @-jezrael,你能解释一下它是如何记住必须保存数据直到看到下一个srv 的吗?
  • @user294110 - 在编辑后的答案中还添加了纯 python 解决方案。
猜你喜欢
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 2014-09-22
相关资源
最近更新 更多