Split a string by a delimiter in python

PythonStringListSplit

Python Problem Overview


How to split this string where __ is the delimiter

MATCHES__STRING

To get an output of ['MATCHES', 'STRING']?

Python Solutions


Solution 1 - Python

You can use the str.split method: string.split('__')

>>> "MATCHES__STRING".split("__")
['MATCHES', 'STRING']

Solution 2 - Python

You may be interested in the csv module, which is designed for comma-separated files but can be easily modified to use a custom delimiter.

import csv
csv.register_dialect( "myDialect", delimiter = "__", <other-options> )
lines = [ "MATCHES__STRING" ]

for row in csv.reader( lines ):
    ...

Solution 3 - Python

When you have two or more elements in the string (in the example below there are three), then you can use a comma to separate these items:

date, time, event_name = ev.get_text(separator='@').split("@")

After this line of code, the three variables will have values from three parts of the variable ev.

So, if the variable ev contains this string and we apply separator @:

Sa., 23. März@19:00@Klavier + Orchester: SPEZIAL

Then, after the split operation the variable

  • date will have value Sa., 23. März
  • time will have value 19:00
  • event_name will have value Klavier + Orchester: SPEZIAL

Solution 4 - Python

For Python 3.8, you actually don't need the get_text method, you can just go with ev.split("@"), as a matter of fact the get_text method is throwing an att. error. So if you have a string variable, for example:

filename = 'file/foo/bar/fox'

You can just split that into different variables with comas as suggested in the above comment but with a correction:

W, X, Y, Z = filename.split('_') 
W = 'file' 
X = 'foo'
Y = 'bar'
Z = 'fox'

Solution 5 - Python

Besides split and rsplit, there is partition/rpartition. It separates string once, but the way question was asked, it may apply as well.

Example:

>>> "MATCHES__STRING".partition("__")
('MATCHES', '__', 'STRING')

>>> "MATCHES__STRING".partition("__")[::2]
('MATCHES', 'STRING')

And a bit faster then split("_",1):

$ python -m timeit "'validate_field_name'.split('_', 1)[-1]"
2000000 loops, best of 5: 136 nsec per loop

$ python -m timeit "'validate_field_name'.partition('_')[-1]"
2000000 loops, best of 5: 108 nsec per loop

Timeit lines are based on this answer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHulkView Question on Stackoverflow
Solution 1 - PythonadamkView Answer on Stackoverflow
Solution 2 - PythonKatrielView Answer on Stackoverflow
Solution 3 - PythonSergey NasonovView Answer on Stackoverflow
Solution 4 - PythonGnaiView Answer on Stackoverflow
Solution 5 - Pythontopin89View Answer on Stackoverflow