English 中文(简体)
阅读档案,只用刀子制造新档案
原标题:Read file, create new file with only the blocks of snippets in it
  • 时间:2012-01-12 20:48:34
  •  标签:
  • python

我有一份案文文件,内容如下:

text texttext texttext texttext texttext text
text texttext texttext texttext text
==========[start log]====
..
..
..
==============


testtextexttexttesttextexttext
testtextexttexttesttextexttext

==========[start log]====
..
..
..
==============

因此,我想创建一份新文件,其中仅包括:

==========[start log]=== 
..
..
..
=============

所有<条码>=栏目中均有2条空白处。

The ... within the/2005/4 is actual text.

最佳回答
import re

regex = re.compile( ==============.+?============== , re.DOTALL)

with open( in.txt ,  r ) as f:
    content = f.read()
matches = regex.findall(content)
问题回答

你可以按行走,只能复制你想要的东西。

old = open( old.txt ,  r )
new = open( new.txt ,  w )

in_block = False

for line in old:
    if line.startswith( ==== ):
        in_block = not in_block # Oposite
        new.write(line)
    elif in_block:
        new.write(line)

old.close()
new.close()

It puts this in new.txt .

==============
..
..
..
==============
==============
..
..
..
==============

《<条码>真正容易::

sed -n  /==============/,/==============/p  example.txt

继承:

==============
..
..
..
==============
==============
..
..
..
==============

从我的理解来看,我认为你试图不把案文列入===_。

raw = open(the_file).read()
parts = raw.split("=============")
new_parts = []
for ii, part in enumerate(parts):
    if not (ii % 2): continue
    new_parts.append(part)
new_raw = "=============".join(new_parts)
open(new_file,  w ).write(new_raw)

我宁愿利用发电机处理一个区块之前材料与一个区块内材料之间的“状态变化”。

你真的读一下头顺序。 标题是(案文==......,案文更多,=......)。 尾矿是头尾部。 定义是详细变造,使我们能够将它固定为一种 lo。

def gen_block_sequence( some_source ):
    block_iter= tier(some_source):
    for line in block_iter:
        if line ==  ============== :
            block= list( gen_head(block_iter)
            yield block

def gen_head( block_iter ):
    for line in block_iter:
        if line ==  ============== :
            break
        yield line

虽然时间很长,但我认为,这很概括,可以容纳更多的处理和决策。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签