Sometimes, we want to split a string by a delimiter in Python
In this article, we’ll look at how to split a string by a delimiter in Python.
How to split a string by a delimiter in Python?
To split a string by a delimiter in Python, we can use the csv module.
For instance, we write
import csv
csv.register_dialect("myDialect", delimiter = "__")
lines = ["MATCHES__STRING"]
for row in csv.reader(lines):
# ...
to callcsv.register_dialect to register a dialect with the delimiter for the CSV file.
Then we call csv.reader with lines to split the strings in lines into the row list by the "__" string.
Conclusion
To split a string by a delimiter in Python, we can use the csv module.