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_ca rriage = line[0:len(line)-1] # remove
carriage
line_without_ca rriage =
line_without_ca rriage.replace( "\\n", "$___n") # replace end of line with
$___n
line_without_ca rriage += "@___n" # add a 'end of line'
character to the end
script.append(l ine_without_car riage)
# 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
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_ca rriage = line[0:len(line)-1] # remove
carriage
line_without_ca rriage =
line_without_ca rriage.replace( "\\n", "$___n") # replace end of line with
$___n
line_without_ca rriage += "@___n" # add a 'end of line'
character to the end
script.append(l ine_without_car riage)
# 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
Comment