0

So currently I am working on some SQL with python which is linked to my database and I am stuck on a split problem. So in the program it connects to the database and then next you input either list, add, update, remove or allocate. So lets say I want to add a new data into the database you just need to write: update -name='Ava - #2' -class=2.

After you type this there is a variable called val which does the strip and split.

So as of right now what I have done is:

val = input('> ').strip().lower()
parts = val.split(' ')
print(parts)

So if I input the following: update -name="Nile Adam" -class=2

I expect the following output: ['update', '-name="Nile Adam"', '-class=2']

However the output I get is: ['update', '-name="Nile', 'Adam"', '-class=2']

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

1 Answers1

0

Simple regex would solve the problem.

Split the word by space but not inside the quotes.

I would split the word using python re package:

import re

>>> re.split('\s(?![\"\w\s\w\"])', 'update -name="Nile Adam" -class=2')

['update', '-name="Nile Adam"', '-class=2']
Debendra
  • 1,132
  • 11
  • 22