0

I'm sure the solution isn't really complicated, but I cant seem to figure it out.

my_string = '2, "some text", "some more text with comma, foo bar", 42, "", 3, 4, "and some more text, hello"'

Want I would like to have:

['2', 'some text', 'some more text with comma, foo bar', '42, '', '3', '4', 'and some more text, hello']

so the problem is that numbers don't have quotation marks, and there can also be empty strings. Basically I want to comma separate the whole string, but not separate the sentences.

Jordy
  • 39
  • 1
  • 4
  • If you split on `"`, you can tell what is in a string and what is not (assuming the quotes are properly balanced) and thus what should be split on a comma and what shouldn't. – Scott Hunter Oct 04 '22 at 12:24
  • A quick workaround for proof-of-concepts, _not for production_: use `ast.literal_eval`. This should return a tuple with the string and the numbers. – Lærne Oct 04 '22 at 12:26
  • @Chris this should not have been marked as duplicate – mcsoini Oct 04 '22 at 12:31
  • 1
    @mcsoini `pyparsing` method in the third answer works as OP expected... I agree it can be bit misleading – Chris Oct 04 '22 at 12:33
  • 1
    @mcsoini I've added another answer where the very first answer addresses the issue. Sorry for the confusion – Chris Oct 04 '22 at 12:39
  • @Chris Thank you Chris, I'll have a look at those answers – Jordy Oct 04 '22 at 12:55

0 Answers0