0

I wanted to ask. if there is an efficient way to split a string and ignore inner strings of is

Example: I get a string in this format:

s = 'name,12345,Hello,\"12,34,56\",World'

and the output I want, is:

['name', '12345', 'Hello', "12,34,56", 'World']

by splitting at the "," the numbers become separated:

['name', '12345', 'Hello', '"12', '34', '56"', 'World']

I know I could split at \" and split the first and the third part separately, but this seems kind of inefficient to me

1
  • Actually, this was the string I wanted to mention... s = 'name,12345,Hello,\\"12,34,56\\",World' big Sorry for that! I edited it above Commented Nov 28, 2022 at 23:39

3 Answers 3

3

This job belongs to the csv module:

import csv

out = next(csv.reader([s], skipinitialspace=True))
# out is
# ['name', '12345', 'Hello', '12,34,56', 'World']

Notes

  • The csv library deals with comma-separated values, perfect for this job
  • It understands quotes, which your input calls for
  • The csv.reader takes in a sequence of texts (i.e. lines of text)
  • The skipinitialspace flag is just that: it tells the reader to skip initial spaces before each value
Sign up to request clarification or add additional context in comments.

Comments

2

If your string separates each element with a comma and a space you could pass ", " to your split method like this:

>>> s = 'name, 12345, Hello, \"12,34,56\", World'
>>> print(s.split(", "))
['name', '12345', 'Hello', '"12,34,56"', 'World']

Comments

1

You can try built-in csv module to parse the string:

import csv
from io import StringIO

s = 'name, 12345, Hello, "12,34,56", World'

print(next(csv.reader(StringIO(s), skipinitialspace=True)))

Prints:

['name', '12345', 'Hello', '12,34,56', 'World']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.