1
def handler_users_answ(coze, res, type, source):
    if res:
        if res.getType() == 'result':
            aa=res.getQueryChildren()
            if aa:
                print 'workz1'
                for x in aa:
                    m=x.getAttr('jid')
                    if m:
                        print m

so this code returns me the values like this:

roomname@domain.com/nickname1
roomname@domain.com/nickname2

and so on, but i want it to print the value after the '/' only. like:

nickname1
nickname2

Thanks in advance.

Wilduck
  • 13,822
  • 10
  • 58
  • 90
MKH
  • 21
  • 2
  • 3
    Why do you use getter/setter methods? That's very unpythonic. If you need getter/setter logic, use properties. – ThiefMaster Aug 08 '11 at 19:50

4 Answers4

2

You can use rpartition to get the part after the last \ in the string.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • I just learned rpartition, always used rsplit(1)[1] for this, nice to know, +1, thanks. – utdemir Aug 08 '11 at 20:07
  • @utdemir: I think the difference is mostly one of style, although a quick `timeit` suggests that in certain cases `rpartition` is a bit faster. – Björn Pollex Aug 08 '11 at 20:14
  • rpartition is incorrect here, because this is a valid JID: `foo@example.com/bar/baz`, and the resource portion is `bar/baz`, not `baz`. – Joe Hildebrand Aug 09 '11 at 02:56
1
a = 'roomname@domain.com/nickname1'

b=a.split('/');
c=b[1];
carlosdc
  • 12,022
  • 4
  • 45
  • 62
1

You can use rsplit which will do the splitting form the right:

a = 'roomname@domain.com/nickname1'
try:
    print a.rsplit('/')[1][1]
except IndexError:
    print "No username was found"

I think that this is efficient and readable. If you really need it to be fast you can use rfind:

a = 'roomname@domain.com/nickname1'
index = a.rfind('/')
if index != -1:
    print a[index+1:]
else:
    print "No username was found"
Alex Plugaru
  • 2,209
  • 19
  • 26
1

To fully parse and validate the JID correctly, see this answer. There's a bunch of odd little edge cases that you might not expect.

Community
  • 1
  • 1
Joe Hildebrand
  • 10,354
  • 2
  • 38
  • 48