# In this exercise, we will parse a format that is based on rfc 822 headers, # though our implementation will only handle the simplest cases. The format # looks like this: # From: Petr Ročkai # To: Random X. Student # Subject: PV248 # and so on and so forth (for your convenience, the above example can be also # found in the file `rfc822.txt`). In real e-mail (and in HTTP), each header # entry may span multiple lines, but we will not deal with that. # Our goal is to create a `dict` where the keys are the individual header # fields and the corresponding values are the strings coming after the colon. # In this iteration, assume that each header is unique. def parse_rfc822( filename: str ) -> dict[ str, str ]: d = {} with open( filename, "r" ) as f: for line in f: parts = line.split( ": ", 1 ) # incl. the space # drop line endings if parts[1][-1] == '\n': parts[1] = parts[1][:-1] d[parts[0]] = parts[1] return d