Arrary replace
#1
guys, sorry if nominclatures are off in this post. here is what i'd like to do, i retreive a list that arrives to python like this:

element1<br>
element2<br>
element3<br>

i want to reformat it to:
element1\n
element2\n
element3\n

this works:
self.final = strinfo[1].replace("</td></tr>","\n ")
                        ^
but only at the single element level ('^')
is there a way to either do a 'foreach' or adjust the string above to do all the elements of the array?

cheers,
-tartag



Reply
#2
(tartag @ nov. 02 2005,13:53 Wrote:this works:
self.final = strinfo[1].replace("</td></tr>","\n ")
^
but only at the single element level ('^')
is there a way to either do a 'foreach' or adjust the string above to do all the elements of the array?

cheers,
-tartag
there are a couple of common styles... pick the one that you like the best. this is in increasing order of sophistication
Quote:resultlist = []
for info in infolist:
resultlist.append(info.replace("</td></tr>","\n "))

or

resultlist = [info.replace("</td></tr>","\n ") for info in infolist]

or

resultlist = map(lambda x: x.replace("</td></tr>","\n "), infolist)
Reply
#3
hmmm,... thanks for the direction and here is what i have done:

housestatusre = re.compile('<tr><td>(.*?)</td></tr>')
housestatus = housestatusre.findall(doc)
resultlist = []
for info in housestatus:
resultlist.append(info.replace("</td></tr>","\n "))
self.mainstat.settext(str(resultlist))

i imagine my problem is: self.mainstat.settext(str(resultlist))
in that i am using a controltextbox... not sure if that is right...

thanks again you are a huge help.
-tags
Reply
#4
does your
.replace("</td></tr>","\n ")
thing really work for these results? it doesn't look like any of the strings you capture in the findall would contain "</td></tr>".

when you cast the array to str the result will look something like

'["text", "text", "text"]'
as opposed to
'text text text'

for your case you should just build the string instead of building the list

Quote:resultstring = ''
for info in housestatus:
resultstring = resultstring + info.replace("</td></tr>","\n ")
self.mainstat.settext(resultstring)
a good way to join the elements of a list together in a string is with the .join method, use it like delimeter.join(list)
so
Quote:', '.join(['red', 'yellow', 'blue'])
is
'red, yellow, blue'
Reply
#5
arghh-

well here is what i did:

for info in housestatus:
title = info
self.statlist.additem(str(title))

it worked!

i appreciate your help, i will forward you a copy of the script in a few.

thanks,
-tartag
Reply

Logout Mark Read Team Forum Stats Members Help
Arrary replace0