Py学习  »  Python

python读取具有启动和停止条件的文件

user294110 • 7 年前 • 2238 次点击  

嗨,我有一个下面的文件数据,我想处理它以获得预期的输出,只是想知道作为一个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

有没有更好的方法来达到这个目的,我对熊猫也没意见。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39554
文章 [ 1 ]  |  最新文章 7 年前
jezrael
Reply   •   1 楼
jezrael    7 年前

使用 Series.str.startswith 具有 Series.cumsum 对于组,然后按 GroupBy.agg 具有 join :

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)