Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to ignore white spaces in double quotes while splitting a string? [duplicate]

Tags:

python

string

I have my data as below

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

I tried to split the line using n=string.split() which gives the below result:

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

I would like to split the string ignoring white spaces in " "

# output expected :

[streptococcus,7120,File being analysed,rd873]
like image 458
user3284648 Avatar asked Dec 06 '25 17:12

user3284648


2 Answers

Use re.findall with a suitable regex. I'm not sure what your error cases look like (what if there are 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.

like image 54
U2EF1 Avatar answered Dec 09 '25 14:12

U2EF1


You want shlex.split, which gives you the behavior you want with the quotes.

import shlex

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

This won't strip extra spaces embedded in the strings, but you can do that with a list comprehension:

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

Look, ma, no regex!

like image 22
kindall Avatar answered Dec 09 '25 16:12

kindall