Home > Software design >  How can I split a string that might have different types of double quotation marks?
How can I split a string that might have different types of double quotation marks?

Time:06-09

I am parsing data in strings that have inconsistent quotation mark types.

One string might only contain regular double quotation marks:

string1 = 'Artist "Title" $12'

The other might contain a double left quotation mark and a double right quotation mark:

string1 = 'Artist “Title” $12'

In either case with double quotation marks, I would like the same outcome:

['Artist ', 'Title', ' $12']

CodePudding user response:

re.split should do the trick, you should split it by any type of quotation

string1 = 'Artist "Title" $12'
string2 = 'Artist “Title” $12'
print(re.split('“|”|"',string1))
print(re.split('“|”|"',string2))

output:

['Artist ', 'Title', ' $12']
['Artist ', 'Title', ' $12']

hope i could help :)

CodePudding user response:

import re

string1 = 'Artist "Title" $12'
string2 = 'Artist “Title” $12'

re.split("[“”'\"]", string1)

Split on a character class with all of the quote characters in it.

  • Related