why is python string split() not splitting

后端 未结 3 2078
执笔经年
执笔经年 2020-11-27 23:34

I have the following python code.

class MainPage(BaseHandler):

    def post(self, location_id):
        reservations = self.request.get_all(\'reservations\'         


        
相关标签:
3条回答
  • 2020-11-28 00:03

    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(' ').

    0 讨论(0)
  • 2020-11-28 00:07

    split does not split the original string, but returns a list

    >>> r  = 'court2 13 0 2012 9 2'
    >>> r.split(' ')
    ['court2', '13', '0', '2012', '9', '2']
    
    0 讨论(0)
  • 2020-11-28 00:27

    Change

    r.split(' ')
    a.split(' ')
    

    to

    r = r.split(' ')
    a = a.split(' ')
    

    Explanation: split doesn't split the string in place, but rather returns a split version.

    From documentaion:

    split(...)
    
        S.split([sep [,maxsplit]]) -> list of strings
    
        Return a list of the words in the string S, using sep as the
        delimiter string.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified or is None, any
        whitespace string is a separator and empty strings are removed
        from the result.
    
    0 讨论(0)
提交回复
热议问题