本文介绍了我如何加速这个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我通过使用os.popen运行命令来获取一些输出。我需要

解析输出并在某种意义上对其进行转换,以便它''''''''''''''''''''''''''''''''''''''''''''''''''''''一个数据库(postgres)
转出一些字符后的
)。因为我是python的新手,所以我不确定如果有更好的方法这样做,那么这就是我所做的:

#Parse the popen返回的输出并返回脚本

out = os.popen(''some command'')

all_lines = out.readlines()


script = []
$ x $ b for x in xrange(len(all_lines)):

line = all_lines [i] .replace("'' "," \\''")[0:len(line)-1]

#replace''with \''

line_without_carriage = line [0:len(line)-1] #remove

运费

line_without_carriage =

line_without_carriage.replace(" \\\ \\ n"," $ ___ n")#replace end of line with

$ ___ n

line_without_carriage + =" @___ n" #添加''行尾''

字符到底

script.append(line_without_carriage)

#end for


script =''''。join(script)


请帮助,因为我很确定我浪费了很多cpu时间在这个循环的
。谢谢


史蒂夫

Hi,

I''m getting some output by running a command using os.popen. I need to
parse the output and transform it in some sense so that it''s ''DB
compatible'', (i.e I need to store the output in a database (postgres)
after escaping some characters). Since I''m new to python, I wasn''t sure
if there was a better way of doing this so this is what I did:
# Parse the output returned by popen and return the script
out = os.popen(''some command'')
all_lines = out.readlines()

script = []
for i in xrange(len(all_lines)):
line = all_lines[i].replace("''", "\\''")[0:len(line)-1]
# replace '' with \''
line_without_carriage = line[0:len(line)-1] # remove
carriage
line_without_carriage =
line_without_carriage.replace("\\n", "$___n") # replace end of line with
$___n
line_without_carriage += "@___n" # add a ''end of line''
character to the end
script.append(line_without_carriage)
# end for

script = ''''.join(script)

Please help because I''m pretty sure I''m wasting a lot of cpu time in
this loop. Thanks

Steve

推荐答案






如果您使用的是Python的DB API 2.0,那么转发将由

完成API:



If you are using Python''s DB API 2.0 than this escaping would be done by
the API:




所以,不需要解析(以及之后的解析)输出 - 我不认为

任何人都可以超过这个速度!


问候,

Marco



So, no need to parse (and afterwards unparse) the ouput - I don''t think
that anyone can beat this speed up!

Regards,
Marco


这篇关于我如何加速这个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 10:03