how to append semicolon to a variable

Y

yaffa

dear folks,

i have the following lines of python code:

couch = incident.findNextSibling('td')
price = couch.findNextSibling('td')
sdate = price.findNextSibling('td')
city = sdate.findNextSibling('td')
strUrl = addr.b.string
currently what this ends up doing is creating something like this

couch3201/01/2004newyork

now what i want to do is add a semicolon after the couch, price, sdate,
city so that i get something like this

couch;32;01/01/2004;new york

does anyone know how to do this?

thanks

yaffa

p.s. i tried couch = couch + ';' and then i tried couch = couch + ";"
and then i tried couch = couch.append ';'
 
G

Grant Edwards

i have the following lines of python code:

couch = incident.findNextSibling('td')
price = couch.findNextSibling('td')
sdate = price.findNextSibling('td')
city = sdate.findNextSibling('td')
strUrl = addr.b.string
currently what this ends up doing is creating something like this

couch3201/01/2004newyork

now what i want to do is add a semicolon after the couch, price, sdate,
city so that i get something like this

couch;32;01/01/2004;new york

Try this:

s = ';'.join([couch,price,sdate,city])
print s
p.s. i tried couch = couch + ';'
and then i tried couch = couch + ";"

both of those should have worked fine.
and then i tried couch = couch.append ';'

1) The append() method of a sequence doesn't return anything.

2) You call methods using ()

3) String are immutable, and therefore don't have an append
method.

Perhaps you ought to read through the tutorial?
 
B

Bengt Richter

dear folks,

i have the following lines of python code:

couch = incident.findNextSibling('td')
price = couch.findNextSibling('td')
sdate = price.findNextSibling('td')
city = sdate.findNextSibling('td')
strUrl = addr.b.string
currently what this ends up doing is creating something like this

couch3201/01/2004newyork

now what i want to do is add a semicolon after the couch, price, sdate,
city so that i get something like this

couch;32;01/01/2004;new york

does anyone know how to do this?

thanks

yaffa

p.s. i tried couch = couch + ';' and then i tried couch = couch + ";"
and then i tried couch = couch.append ';'
Assuming all your findNextSibling calls result in strings, as in

You can put them in a list
>>> [couch, price, sdate, city]
['couch', '32', '01/01/2004', 'newyork']

And join them with a string inserted between each successive pair of elements
>>> ';'.join([couch, price, sdate, city])
'couch;32;01/01/2004;newyork'

If you like a space after each ';', just include it in the joiner string
>>> '; '.join([couch, price, sdate, city]) 'couch; 32; 01/01/2004; newyork'
>>>

If all your items weren't string type, you'll have to convert first, e.g.,
>>> price = 32
>>> '; '.join([couch, price, sdate, city])
Traceback (most recent call last):
File said:
>>> '; '.join(map(str,[couch, price, sdate, city]))
'couch; 32; 01/01/2004; newyork'

If you have 2.4 and don't like map, you can use a generator expression:
'couch; 32; 01/01/2004; newyork'

or if you want the quoted strings
"'couch'; 32; '01/01/2004'; 'newyork'"

Note: if you want ';' appended to each item instead of just between items, you
can just add ';' to the whole thing
'couch; 32; 01/01/2004; newyork;'

Which avoids the trailing blank you would get if you appended '; ' to every item
before joining them, as in
'couch; 32; 01/01/2004; newyork; '

or
'couch; 32; 01/01/2004; newyork; '

HTH

Regards,
Bengt Richter
 
T

tiissa

Grant said:
i have the following lines of python code:

couch = incident.findNextSibling('td')
price = couch.findNextSibling('td')
sdate = price.findNextSibling('td')
city = sdate.findNextSibling('td')
strUrl = addr.b.string
currently what this ends up doing is creating something like this

couch3201/01/2004newyork

now what i want to do is add a semicolon after the couch, price, sdate,
city so that i get something like this

couch;32;01/01/2004;new york


Try this:

s = ';'.join([couch,price,sdate,city])
print s

I'll risk myself with something like:

s = ';'.join([tag.string for tag in [couch,price,sdate,city]])

Of course, from the question I wouldn't have any clue. I just like doing
some guessing on problems I know nothing about. ;)
both of those should have worked fine.

Not really. It seems to me the OP is using BeautifulSoup (or some other
SGML parser). In this case, couch and others are not strings but objects.
It may also be that strUrl is their parent (but I wouldn't know, how
would I?)
Perhaps you ought to read through the tutorial?

That's always useful.
However, the first thing is to put the minimal context in your question
to help the people you want your answers from understanding your issue.
I would advise you to read tutorials and documentations on the modules
you're using as well as learning to ask meaningful questions[1].


[1] http://www.catb.org/~esr/faqs/smart-questions.html
 
G

Grant Edwards

Grant Edwards wrote:
s = ';'.join([couch,price,sdate,city])
print s

I'll risk myself with something like:

s = ';'.join([tag.string for tag in [couch,price,sdate,city]])

Of course, from the question I wouldn't have any clue. I just like doing
some guessing on problems I know nothing about. ;)
both of those should have worked fine.

Not really. It seems to me the OP is using BeautifulSoup (or some other
SGML parser). In this case, couch and others are not strings but objects.

Seems like a reasonable guess.
It may also be that strUrl is their parent (but I wouldn't know, how
would I?)

Nope. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top