English 中文(简体)
相当于 per——pe吗?
原标题:Python equivalent to perl -pe?

在一些文本档案中,我需要收集一些数字。 我可以总结一下我需要的线索,但不知道如何从线上提取数字。 一位同事向我展示了如何用 per子做到这一点:

cat results.txt | perl -pe  s/.+(d.d+).
/1 / 

然而,我通常在Sharma,而不是Perl。 因此,我的问题是,我是否可以以同样的方式使用沙捞? I.e.,我是否可以把一些东西从巴什拖到沙里,然后将结果直截了当地推倒? ......如果是这样的话。 或者,在这种情况下,Perl就更方便了吗?

最佳回答

是的,你可以从指挥线上使用沙捞。 <代码>python -c <stuff>将操作<stuff>,作为<代码。 例:

python -c "import sys; print sys.path"

相对于“-p 选择Perl(自动投入/产出线-逐行处理)而言,有直接对应的斜体,但主要因为“Z”没有使用<代码>的相同概念,即,而且“Perl”没有使用——在所有投入和产出都是人工操作的(通过raw_input(>>>>>>>input(>>>,>,和>>>>。


例如:

cat results.txt | python -c "import re, sys; print   .join(re.sub(r .+(d.d+).
 , r 1  , line) for line in sys.stdin)"

(难免) 或许最好写一下文字,以便在实际的沙里面做。

问题回答

您可以使用:

$ python -c  <your code here> 

理论上,你可以说,但沙尔 t在靠近Perl的reg魔的任何地方,因此,由此产生的指挥将更加无能为力,特别是因为你可以不进口<<>re>>而使用常规表述。 (你很可能需要<代码>sys,sys.stdin。)

你们同事的Perl一只一只lin子大致如下:

import sys, re
for line in sys.stdin:
    print re.sub(r .+(d.d+).
 , r 1  , line)

你的问题可以以几种方式解决。

我认为,你应考虑从沙尔直接使用定期表达(你就是这样说的)。 定期表述载于<代码>re模块。 例如:

import re
filecontent = open( somefile.txt ).read()
print re.findall( .+(d.d+).$ , filecontent)

(I would prefer using $ instead of for line endings, because line endings are different between operational systems and file encodings)

如果你想要从山谷内打巴什指挥部,你可以使用:

import os
os.system(mycommand)

如果指挥是巴什指挥部。 我用了这段时间,因为有些行动比灰色更好地进行。

最后,如果你想用灰色提取数字,则使用<代码>-o的备选办法,该办法只印刷了相应的部分。

Perl(或ed)更方便。 然而,如果可能的话:

python -c  import sys, re; print "
".join(re.sub(".+(d.d+).
","1 ", l) for l in sys.stdin) 

引用https://stackoverflow.com/a/12259852/411282:

for ln in __import__("fileinput").input(): print ln.rstrip()

见上文相关解释,但这在<代码>perl-p上做了许多工作,包括支持多个档案名称和<代码>stdin。 在没有档案名称的情况下。





相关问题
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 ]="...

热门标签