[英]Read until empty line and save as new file - python
first of all, completely new to python, so this question might be easy.首先,对 python 来说是全新的,所以这个问题可能很简单。
I want to read a text file and save it as a new file, separate the header in emails from the body.我想读取一个文本文件并将其保存为一个新文件,将电子邮件中的 header 与正文分开。 The place where the header ends is the empty line under "X-UID: 81" (as seen in the image).
header 结束的地方是“X-UID:81”下的空行(如图所示)。 Not all emails have the "X-UID:" so the empty line is the place where I want to separate it.
并非所有电子邮件都有“X-UID:”所以空行是我想要分隔它的地方。 Is there an easy way to do this?
是否有捷径可寻?
My code currently looks like this:我的代码目前如下所示:
with open("1.txt") as fReader:
corpus = fReader.read()
loc = corpus.find("X-UID")
print(corpus[:loc])
One way to do this is to read the entire file as one string, and split it by X-UID: 81
(provided that this substring is present, of course), like so:一种方法是将整个文件作为一个字符串读取,然后按
X-UID: 81
拆分它(当然,前提是存在此 substring),如下所示:
parts = s.split('X-UID: 81')
header, body = parts[0], parts[1]
If the file doesn't contain X-UID: 81
, you could just split()
by double newline ( \n\n
) with maxsplit=1
to make sure it doesn't split further on the newlines in the email body:如果文件不包含
X-UID: 81
,您可以使用maxsplit=1
通过双换行符 ( \n\n
) split()
以确保它不会在 email 正文中的换行符上进一步拆分:
parts = s.split('\n\n', maxsplit=1)
header, body = parts[0], parts[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.