Python - How to ignore spaces in double quotes when splitting a string?

I have the data below

string = ' streptococcus 7120 "File  being  analysed" rd873 '

I tried breaking a string using n=string.split(), which gives the following result:

[streptococcus,7120,File,being,analysed,rd873]

I would like to break a line that ignores spaces in "

# output expected :

[streptococcus,7120,File being analysed,rd873]
+3
source share
2 answers

Use re.findallwith a suitable regular expression. I'm not sure what your errors look like (what if there is an odd number of quotes?), But:

filter(None, it.chain(*re.findall(r'"([^"]*?)"|(\S+)', ' streptococcus 7120 "File  being  analysed" rd873 "hello!" hi')))
> ['streptococcus',
   '7120',
   'File  being  analysed',
   'rd873',
   'hello!',
   'hi']

looks right.

+4
source

You want shlex.splitone that gives you the behavior you want with quotes.

import shlex

string = ' streptococcus 7120 "File  being  analysed" rd873 '
items  = shlex.split(string)

This will not separate the extra spaces embedded in the lines, but you can do it with a list:

items  = [" ".join(x.split()) for x in shlex.split(string)]

Look ma, no regex!

+3
source

All Articles