I'm writing a script to parse an email, but there is some SyntaxError
on the for loop in the following part:
def main():
writer = csv.DictWriter(open('features.csv', 'w'), list(EXTRACTS.keys()))
for mail in os.listdir(MAILDIR):
writer.writerow({
key: value(email.message_from_file(open(os.path.join(MAILDIR, mail), 'r')))
for key, value in EXTRACTS.items()
})
Please help me out of this!
EDIT:
File "/IS/extraction.py", line 52
for key, value in EXTRACTS.items() ^ SyntaxError: invalid syntax
You are running this on an older Python version that doesn't yet support dict comprehensions. The
{key: value for ... in ...}
syntax is only available in Python 2.7 and newer:Replace the line with a dictionary constructor and a generator expression:
You do want to avoid reading the email message for every key-item pair in
EXTRACTS
though; read it once per outer loop: