Reputation: 13
I have the folllowing line of code
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
for line in handle:
if line.startswith('From '):
value= (line.split()[1])
print(value)
And as a output I get the mails adresses in the text file in the following format:
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
Does anybody know how to get the output in a normal list format like ['louis@media.berkeley.edu' , 'rjlowe@iupui.edu' ......]
Thanks for your help!
Upvotes: 0
Views: 52
Reputation: 31
Use Comprehension:
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
value= [line.split()[1] for line in handle if line.startswith('From ')]
print(value)
Upvotes: 1
Reputation: 13
I am pretty sure that the easiest way to do that is:
handle = open(name, "r")
content = handle.read()
content_list = content.split("\n\n")
handle.close()
print(content_list)
Upvotes: 0