Quantcast
Viewing latest article 37
Browse Latest Browse All 51

Answer by Louis Maddox for Parse mailto urls in Python

I like Alexander's answer but it is in Python 2! We now get urlparse() and parse_qs() from urllib.parse. Also note that sorting the header in reverse puts it in the order: to, from, body.

from email.message import Messagefrom pathlib import Pathfrom urllib.parse import parse_qs, urlparseurl = Path("link.txt").read_text()msg = Message()parsed_url = urlparse(url)header = parse_qs(parsed_url.query)header["to"] = header.get("to", []) + parsed_url.path.split(",")for k, v in sorted(header.items(), reverse=True):    print(f"{k}:", v[0])

I am just using this as a one-off, when I used msg.as_string() I got some strange results though so I just went with the string. The values are lists of one value so I access the 0'th entry to make it a string.


Viewing latest article 37
Browse Latest Browse All 51

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>