Split a string by spaces — preserving quoted substrings — in Python

后端 未结 16 865
心在旅途
心在旅途 2020-11-22 15:05

I have a string which is like this:

this is \"a test\"

I\'m trying to write something in Python to split it up by space while ignoring spac

16条回答
  •  一个人的身影
    2020-11-22 15:49

    If you don't care about sub strings than a simple

    >>> 'a short sized string with spaces '.split()
    

    Performance:

    >>> s = " ('a short sized string with spaces '*100).split() "
    >>> t = timeit.Timer(stmt=s)
    >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
    171.39 usec/pass
    

    Or string module

    >>> from string import split as stringsplit; 
    >>> stringsplit('a short sized string with spaces '*100)
    

    Performance: String module seems to perform better than string methods

    >>> s = "stringsplit('a short sized string with spaces '*100)"
    >>> t = timeit.Timer(s, "from string import split as stringsplit")
    >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
    154.88 usec/pass
    

    Or you can use RE engine

    >>> from re import split as resplit
    >>> regex = '\s+'
    >>> medstring = 'a short sized string with spaces '*100
    >>> resplit(regex, medstring)
    

    Performance

    >>> s = "resplit(regex, medstring)"
    >>> t = timeit.Timer(s, "from re import split as resplit; regex='\s+'; medstring='a short sized string with spaces '*100")
    >>> print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
    540.21 usec/pass
    

    For very long strings you should not load the entire string into memory and instead either split the lines or use an iterative loop

提交回复
热议问题