I have strings that need to be placed into lists; for instance I require that
C C .0033 .0016 'International Tables Vol C Tables 4.2.6.8 and 6.1.1.4' C
becomes
['C', 'C', '.0033', '.0016', 'International Tables Vol C Tables 4.2.6.8 and 6.1.1.4', 'C']
So everything in quotes becomes a single list element; otherwise, everything separated by whitespace becomes a single list element.
My first idea was a simple split, place the items that don't contain '
into a new array, and then place the ones that are in a quoted-section back together:
>>> s.split()
['C', 'C', '.0033', '.0016', "'International", 'Tables', 'Vol', 'C', 'Tables', '4.2.6.8', 'and', "6.1.1.4'", 'C']
>>> arr = []
>>> i = 0
>>> while i < len(s):
v = ''
if s[i].startswith("'"):
while not s[i].endswith("'"):
v = v.append(s[i]+ " ")
i += 1
v.append(s[i])
arr.append(v)
else:
arr.append(s[i])
But this strategy is pretty ugly, and in addition I have to assume that the string was split on a single space.
s.partition("'")
seemed very promising:
>>> s.partition("'")
('C C .0033 .0016 ', "'", "International Tables Vol C Tables 4.2.6.8 and 6.1.1.4' C")
but it's awkward because I have to partition again as I iterate through, and it's context-sensitive as to which one was in quotes.
Is there a simple Python3 way to split this string as described above?