Split a string by a delimiter in 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 ):
...
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 valueSa., 23. März
time
will have value19:00
event_name
will have valueKlavier + Orchester: SPEZIAL
You can use the str.split
method: string.split('__')
>>> "MATCHES__STRING".split("__")
['MATCHES', 'STRING']