Python separates the string with spaces, except in quotation marks, but retains quotation marks

I want to break the following line:

Quantity [*, 'EXTRA 05', *]

With the desired results:

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

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

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

Any suggestions are welcome.

EDIT:

Multiple splits will also be required, such as:

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

To:

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

+1
3

(module re)

, ( , ), :

import re

r = re.compile('(?! )[^[]+?(?= *\[)'
               '|'
               '\[.+?\]')


s1 = "Quantity [*,'EXTRA 05',*] [*,'EXTRA 09',*]"
print r.findall(s1)
print '---------------'      

s2 = "'zug hug'Quantity boondoggle 'fish face monkey "\
     "dung' [*,'EXTRA 05',*] [*,'EXTRA 09',*]"
print r.findall(s2)

['Quantity', "[*,'EXTRA 05',*]", "[*,'EXTRA 09',*]"]  
---------------
["'zug hug'Quantity boondoggle 'fish face monkey dung'", "[*,'EXTRA 05',*]", "[*,'EXTRA 09',*]"]

undesrtood :

'|' OR

, RE:
(?! )[^[]+?(?= *\[)

\[.+?\]

RE:

[^[]+
. ^ [, , , , ^.
[^[] , [ , + , [^[]+ , .

[^[]+ : , , .
, ? - (?= *\[), , (?=....), , *\[, , . *\[ : , ( \, [ ).

(?! ) , : RE, , , . (?! ), .

RE:

\[.+?\] : characater [, , .+? ( , \n)), ], , .

.

string = "Quantity [*,'EXTRA 05',*] [*,'EXTRA 09',*]"
import re
print re.split(' (?=\[)',string)

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

!!

+4

, , , :

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

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

"Quantity [*,'EXTRA 05',*] [*,'EXTRA 10',*] [*,'EXTRA 07',*] [*,'EXTRA 09',*]"

string = "Quantity [*,'EXTRA 05',*] [*,'EXTRA 09',*]"
splitted_string = []

#This adds "Quantity" to the position 0 of splitted_string
splitted_string.append(string.split(" ")[0])     

#The for goes from 1 to the lenght of string.split(" "),increasing the x by 2
#The first iteration x is 1 and x+1 is 2, the second x=3 and x+1=4 etc...
#The first iteration concatenate "[*,'EXTRA" and "05',*]" in one string
#The second iteration concatenate "[*,'EXTRA" and "09',*]" in one string
#If the string would be bigger, it will works
for x in range(1,len(string.split(" ")),2):
    splitted_string.append("%s %s" % (string.split(" ")[x],string.split(" ")[x+1]))

, :

['Quantity', "[*,'EXTRA 05',*]", "[*,'EXTRA 09',*]"]
splitted_string[0] = 'Quantity'
splitted_string[1] = "[*,'EXTRA 05',*]"
splitted_string[2] = "[*,'EXTRA 09',*]"

, , . , . ,

+1

, , : Python , , .

. , , Python, . , , .

This is the code that I shot down to do this, it has not been extensively tested:

def spaceSplit(string) :
  last = 0
  splits = []
  inQuote = None
  for i, letter in enumerate(string) :
    if inQuote :
      if (letter == inQuote) :
        inQuote = None
    else :
      if (letter == '"' or letter == "'") :
        inQuote = letter

    if not inQuote and letter == ' ' :
      splits.append(string[last:i])
      last = i+1

  if last < len(string) :
    splits.append(string[last:])

  return splits
0
source

Source: https://habr.com/ru/post/1621536/


All Articles