Home > Net >  Python - split string without inner string
Python - split string without inner string

Time:11-29

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

CodePudding user response:

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

CodePudding user response:

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']

CodePudding user response:

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']
  • Related