[Python] Python split string by spaces except when in quotes, but keep the quotes

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    Am wanting to split the following string:


    Quantity [*,'EXTRA 05',*]
    ​

    With the desired results being:


    ["Quantity", "[*,'EXTRA 05',*]"]
    ​

    The closest I have found is using shlex.split, however this removes the internal quotes giving the following result:


    ['Quantity', '[*,EXTRA 05,*]']
    ​

    Any suggestions would be greatly appreciated.

    EDIT:

    Will also require multiple splits such as:


    "Quantity [*,'EXTRA 05',*] [*,'EXTRA 09',*]"
    ​

    To:


    ["Quantity", "[*,'EXTRA 05',*]", "[*,'EXTRA 09',*]"]
    ​

    Continue reading...

Compartilhe esta Página