简体   繁体   English

读到空行并保存为新文件 - python

[英]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]) 
  • This sort of works, but I can't separate at the empty line.这种工作,但我不能在空行分开。 And don't know how to save as new file而且不知道如何另存为新文件

Example email示例 email

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM