How do I speedup this loop?
Riccardo Attilio Galli
riccardo_cut-me at cut.me.sideralis.net
Tue Jul 13 07:55:06 EDT 2004
On Tue, 13 Jul 2004 16:48:36 +1000, Steve wrote:
> 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:
if you were replacing a character with another, the best and quick way was
to use a translation table, but you're not lucky
> line = all_lines[i].replace("'", "\\'")[0:len(line)-1]
> # replace ' with \'
? this can't work. You never defined "line" and you're using "len" on it.
I think you want to delete the last character of the string. if so, you
can use negative indexes
line = all_lines[i].replace("'", "\\'")[:-1]
the 0 disappeared is the default value
> line_without_carriage = line[0:len(line)-1] # remove carriage
similar here
line_without_carriage = line[:-1]
but you're just deleting the current last character of the string.
so you could delete this line and change the indexing in the first one
so the first one would become
line = all_lines[i].replace("'", "\\'")[:-2]
ah, I don't think you're removing a carriage return ('\r') here.
If your line end with '\r\n' you're killing '\n' , a line feed.
This is important 'cause in the next line....
> line_without_carriage = line_without_carriage.replace("\\n", "$___n")
# replace end of line with $___n
... you try to replace '\\n' ,
are you intending to delete the line feed, the end of line ?
if this is the case you should write '\n' (one character) not '\\n' (a
string of len 2)
> line_without_carriage += "@___n"
> script.append(line_without_carriage)
> # end for
> script = ''.join(script)
the best here is to do
script.append(line_without_carriage)
script.append('@___n')
# end for
script = ''.join(script)
Appending '@___n' you don't need to loose memory for destroying and
creating a new string each time
> Please help because I'm pretty sure I'm wasting a lot of cpu time in
> this loop. Thanks
>
> Steve
Ciao,
Riccardo
--
-=Riccardo Galli=-
_,e.
s~ ``
~@. ideralis Programs
. ol
`**~ http://www.sideralis.net
More information about the Python-list
mailing list