【问题标题】:Create Sections in Python在 Python 中创建部分
【发布时间】:2013-08-04 08:06:43
【问题描述】:

我是 Python 的新手。我通过日志有带有重复字符串的大文件

例子:

abc
def
efg
gjk
abc
def
efg
gjk
abc
def
efg
gjk
abc
def
efg
gjk

预期结果

--------------------Section1---------------------------
abc
def
efg
gjk
--------------------Section2---------------------------
abc
def
efg
gjk
--------------------Section3---------------------------
abc
def
efg
gjk
--------------------Section4---------------------------
abc
def
efg
gjk

能否为我提供继续此操作的指针。 我为特定的字符串尝试了 grep,它只给了我特定顺序的字符串。 我想把从 abc 到 gjk 的整个日志放在一个部分中。

【问题讨论】:

  • 一个部分是否总是以相同的字符串开头?
  • 切片的规则是什么?每四行一节?或者创建四个等长的部分?还是……?
  • 每个部分都包含行中列出的字符串,但不以该字符串开头
  • 是的。每 4 行一节

标签: python


【解决方案1】:

如果一个部分由起始行定义,您可以使用生成器函数从输入迭代中产生部分:

def per_section(iterable):
    section = []
    for line in iterable:
        if line.strip() == 'abc':
            # start of a section, yield previous
            if section:
                yield section
            section = []

        section.append(line)

    # lines done, yield last
    if section:
        yield section

将此与输入文件一起使用,例如:

with open('somefile') as inputfile:
    for i, section in enumerate(per_section(inputfile)):
        print '------- section {} ---------'.format(i)
        print ''.join(section)

如果部分只是基于行数,请使用itertools grouper recipe 将输入可迭代分组为固定长度的组:

from itertools import izip_longest

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

with open('somefile') as inputfile:
    for i, section in enumerate(grouper(inputfile, 4, '\n')):
        print '------- section {} ---------'.format(i)
        print ''.join(section)

【讨论】:

    【解决方案2】:

    为简单起见(如您所说:每 4 行一条记录):

    with open ('yourfile', 'r') as f: lines = [x for x in f]
    while lines:
        print ('----------------------------------')
        print (lines [:4] )
        lines = lines [4:]
    

    【讨论】:

      【解决方案3】:

      由于您有一个已知的起点,因此您可以在看到部分开始时触发部分标题:

      >>> section = 0
      >>> with open('bigdata.txt') as f:
              for line in f:
                  if 'abc' in line:
                      section += 1
                      print ('Section' + str(section)).center(55, '-')
                  print line
      
      
      ------------------------Section1-----------------------
      abc
      def
      efg
      gjk
      ------------------------Section2-----------------------
      abc
      def
      efg
      gjk
      ------------------------Section3-----------------------
      abc
      def
      efg
      gjk
      ------------------------Section4-----------------------
      abc
      def
      efg
      gjk
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-24
        • 2013-01-17
        • 1970-01-01
        相关资源
        最近更新 更多