Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

http - why is python string split() not splitting

I have the following python code.

class MainPage(BaseHandler):

    def post(self, location_id):
        reservations = self.request.get_all('reservations')
        for r in reservations:
            a=str(r)
            logging.info("r: %s " % r)
            logging.info("lenr: %s " % len(r))
            logging.info("a: %s " % a)
            logging.info("lena: %s " % len(a))
            r.split(' ')
            a.split(' ')
            logging.info("split r: %s " % r)
            logging.info("split a: %s " % a)

I get the following log printout.

INFO     2012-09-02 17:58:51,605 views.py:98] r: court2 13 0 2012 9 2 
INFO     2012-09-02 17:58:51,605 views.py:99] lenr: 20 
INFO     2012-09-02 17:58:51,605 views.py:100] a: court2 13 0 2012 9 2 
INFO     2012-09-02 17:58:51,606 views.py:101] lena: 20 
INFO     2012-09-02 17:58:51,606 views.py:108] split r: court2 13 0 2012 9 2 
INFO     2012-09-02 17:58:51,606 views.py:109] split a: court2 13 0 2012 9 2 

I get the same log printout if instead of split(' ') I use split(), btw.

Why is split not splitting the result into a list with 6 entries? I suppose the problem is that http request is involved, because my tests in the gae interactive console get the expected result.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

split does not modify the string. It returns a list of the split pieces. If you want to use that list, you need to assign it to something with, e.g., r = r.split(' ').


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...