Python Tip #305 (of 365): Split only as much as you need with maxsplit. Do this:...

@treyhunner@mastodon.social

Python Tip #305 (of 365):

Split only as much as you need with maxsplit.

Do this:
key, value = line.split("=", maxsplit=1)

Not this:
key, *rest = line.split("=")
value = "=".join(rest)

Calling split without maxsplit breaks when the value contains the separator:

>>> line = "API_KEY=3q2+7w=="
>>> key, value = line.split("=")
Traceback (most recent call last):
...
ValueError: too many values to unpack (expected 2, got 4)

๐Ÿงต (1/3)

#Python #DailyPythonTip