我如何加速这个循环? [英] How do I speedup this loop?

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

问题描述




我通过使用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

推荐答案

___ n")#replace end of line with the br />
___n") # replace end of line with


___ n

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

字符到底

script.append(line_without_carriage)

#end for


script =''''。join(script)


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


Steve

___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


2004年7月13日星期二16:48:36 +1000,未知< un*****@unknown.invalid>

写道:
On Tue, 13 Jul 2004 16:48:36 +1000, Unknown <un*****@unknown.invalid>
wrote:
我通过使用os.popen运行命令得到一些输出。我需要解析输出并在某种意义上对其进行转换,以便它''DB
兼容'',(即我需要将输出存储在数据库中(postgres)
转义一些字符后。)
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).




如果您使用的是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:

import odbc,dbi
con = odbc.odbc(" DB_ID / USERNAME / PASSWORD")
cur = con.cursor()
sql =" INSERT INTO输出(行)VALUES(?)"
dirty_line =''带有禁止字符的一些文字\ n \r ...''
cur.execute(sql,dirty_line)
import odbc,dbi
con = odbc.odbc("DB_ID/USERNAME/PASSWORD")
cur = con.cursor()
sql = "INSERT INTO output (line) VALUES (?)"
dirty_line = ''Some text with forbidden characters\n\r...''
cur.execute(sql, dirty_line)




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

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


问候,

Marco



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

Regards,
Marco


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

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