How do I parse connection strings in Python?

Viewed 19

I am looking for a way to redact the password when logging errors, but still write out the rest of a connection string like

DSN=Something;UID=user;PWD=topsecret

That is, a connection string following most likely the rules described here.

The top answer when looking for this suggests using urllib.parse, but this only works for connection strings that are set up like a URL, not the one described above.

My first attempt would be to separate by ; and then by = and match the keys for either pwd or password after converting it to lower keys, but this will not work if, for example, the value itself contains a semicolon, which would look like

UID=user;PWD="my;password"

Is there a better way than writing a full parser with all eventualities covered by myself, which I imagine might be error prone?

0 Answers
Related