[英]Read emails from txt file using python
我有一个带有电子邮件的 TXT 文件,例如:
From r Wed Oct 30 21:41:56 2002
Return ...
...
From r Thu Oct 31 08:11:39 2002
Return ...
...
我想将每个 email 提取到一个数组中,例如:
["From r Wed Oct 30 21:41:56 2002 Return ...", "From r Thu Oct 31 08:11:39 2002 Return ...", ..., "From r ..."]
我正在使用 python
with open(self.file, encoding="utf8", errors='ignore') as data_file:
lines = ''
first_line = True
for line in data_file:
if line.startswith("From r") and not first_line:
emails.append(lines)
lines = ''
else:
first_line = False
lines = lines + line
Assuming the first line of every email begins with From r
, we can loop over each line of the email, adding a new entry to the list of emails every time we see From r
, and concatenating every line after that to the "current" email由索引i
跟踪。
emails = []
with open('emails.txt') as f:
i = -1
for line in f:
if line.startswith('From r'):
emails.append(line)
i += 1
else:
emails[i] += line
print(emails)
输出:
['From r Wed Oct 30 21:41:56 2002\nReturn ...\n...\n', 'From r Thu Oct 31 08:11:39 2002\nReturn ...\n...\n']
尝试这个:
emails_list = []
email = ""
with open("full/path/to/file", "r") as f:
email += f.readline()
for l in f.readlines():
l = l.strip()
if not l.startswith("From r"):
email += " " + l
else:
emails_list.append(email)
email = l
else:
emails_list.append(email)
print(emails_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.