paseline(我最喜欢的简单脚本):是否存在类似的东西? [英] paseline(my favorite simple script): does something similar exist?

查看:95
本文介绍了paseline(我最喜欢的简单脚本):是否存在类似的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最喜欢的一个脚本是parseline,它是打印的

以下


def parseline(行,格式):

xlat = {''x'':无,'s'':str,''f'':float,''d'':int,''i'':int}

result = []

words = line.split()

for i in range(len(format)):

f = format [i]

trans = xlat.get(f,''None'')

if trans:result.append(trans(words [i]) )

if len(result)== 0:return None

if len(result)== 1:return result [0]

返回结果


这需要一行文本,拆分它,然后应用简单的

格式化字符来返回不同的python类型。例如,

给定该行


H 0.000 0.000 0.000


我可以调用parseline(行,' 'sfff'')它将返回字符串''H'',

和三个花车。如果我想省略第一个,我可以调用

parseline(line,''xfff'')。如果我只想要前0.000,我可以调用

parseline(行,''xf'')。很明显,我没有用这种方式完成所有解析,

但是我发现parseline在很多应用程序中很有用。


我是在这里张贴这个因为(1)我感到自鸣得意的是,我是一个多么聪明的小编码器,以及(2)(在一个更加现实和谦逊的框架中

mind)我意识到很多人可能已经找到了类似需求的解决方案,而且我会想象许多人比上面的更好。我很想听听别人怎么做类似的事情。


Rick

One of my all-time favorite scripts is parseline, which is printed
below

def parseline(line,format):
xlat = {''x'':None,''s'':str,''f'':float,''d'':int,''i'':int}
result = []
words = line.split()
for i in range(len(format)):
f = format[i]
trans = xlat.get(f,''None'')
if trans: result.append(trans(words[i]))
if len(result) == 0: return None
if len(result) == 1: return result[0]
return result

This takes a line of text, splits it, and then applies simple
formatting characters to return different python types. For example,
given the line

H 0.000 0.000 0.000

I can call parseline(line,''sfff'') and it will return the string ''H'',
and three floats. If I wanted to omit the first, I could just call
parseline(line,''xfff''). If I only wanted the first 0.000, I could call
parseline(line,''xf''). Clearly I don''t do all of my parsing this way,
but I find parseline useful in a surprising number of applications.

I''m posting this here because (1) I''m feeling smug at what a bright
little coder I am, and (2) (in a more realistic and humble frame of
mind) I realize that many many people have probably found solutions to
similar needs, and I''d imaging that many are better than the above. I
would love to hear how other people do similar things.

Rick

推荐答案

" RickMuller" < rp ****** @ gmail.comwrites:
"RickMuller" <rp******@gmail.comwrites:

def parseline(行,格式):

xlat = {' 'x'':无,'s'':str,''f'':float,''d'':int,''i'':int}

result = [ ]

words = line.split()

for i in range(len(format)):

f = format [i]

trans = xlat.get(f,''None'')

if trans:result.append(trans(words [i]))

如果len(结果)== 0:返回无

如果len(结果)== 1:返回结果[0]

返回结果
def parseline(line,format):
xlat = {''x'':None,''s'':str,''f'':float,''d'':int,''i'':int}
result = []
words = line.split()
for i in range(len(format)):
f = format[i]
trans = xlat.get(f,''None'')
if trans: result.append(trans(words[i]))
if len(result) == 0: return None
if len(result) == 1: return result[0]
return result



未经测试,但可能更多当前的Pythonic风格:


def parseline(行,格式):

xlat = {''x'':无,'s'':str,''f'':float,''d'':int,''我':int}

结果= []

words = line.split()

for f,w in zip(格式,单词):

trans = xlat [ f]

如果trans不是t无:

result.append(trans(w))

返回结果


差异:

- 不会忽略不正确的格式字符,而是引发异常

- 总是返回列表中的值,包括作为空列表,如果

那就没有了值$ / b $ b - 使用迭代器协议和zip来避免丑陋的索引变量

和下标

Untested, but maybe more in current Pythonic style:

def parseline(line,format):
xlat = {''x'':None,''s'':str,''f'':float,''d'':int,''i'':int}
result = []
words = line.split()
for f,w in zip(format, words):
trans = xlat[f]
if trans is not None:
result.append(trans(w))
return result

Differences:
- doesn''t ignore improper format characters, raises exception instead
- always returns values in a list, including as an empty list if
there''s no values
- uses iterator protocol and zip to avoid ugly index variable
and subscripts


Hi Rick,


确实很好的小脚本!


你可能意味着
Hi Rick,

Nice little script indeed !

You probably mean

trans = xlat。 get(f,None)
trans = xlat.get(f,None)



而不是

instead of


trans = xlat.get(f,'''没有'')
trans = xlat.get(f,''None'')



在提供无效格式字符的情况下。字符串

''无''评估为True,因此trans(words [i])引发异常


一个变体,具有列表理解而不是for循环:


def parseline(行,格式):

xlat = {''x'':无,'s'' :str,''f'':float,''d'':int,''i'':int}

result = []

words = line .split()

result = [xlat [f](w)for f,w in zip(格式,单词)

if xlat.get(f,None) ]

如果不是结果:返回无

如果len(结果)== 1:返回结果[0]

返回结果


问候,

Pierre

in the case where an invalid format character is supplied. The string
''None'' evaluates to True, so that trans(words[i]) raises an exception

A variant, with a list comprehension instead of the for loop :

def parseline(line,format):
xlat = {''x'':None,''s'':str,''f'':float,''d'':int,''i'':int}
result = []
words = line.split()
result = [ xlat[f](w) for f,w in zip(format,words)
if xlat.get(f,None) ]
if not result: return None
if len(result) == 1: return result[0]
return result

Regards,
Pierre


RickMuller写道:
RickMuller wrote:

我最喜欢的脚本之一是parseline,打印
One of my all-time favorite scripts is parseline, which is printed



这是写另一种方式:


def parseline(行,格式):

trans = {''x'':lambda x:None,'s'': str,''f'':float,''d'':int,''i'':int}

返回[trans [f](w)for f,w in zip( format,line.split())]

here is another way to write that:

def parseline(line, format):
trans = {''x'':lambda x:None,''s'':str,''f'':float,''d'':int,''i'':int}
return [ trans[f](w) for f,w in zip(format, line.split() ) ]


>> parseline(''A 1 22 3 6'',''sdxf'')
>>parseline( ''A 1 22 3 6'', ''sdxf'')



[''A'',1,无,3.0]

I.

[''A'', 1, None, 3.0]
I.


这篇关于paseline(我最喜欢的简单脚本):是否存在类似的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆