如何在Python中从文本文件中删除标题?

如何在Python中从文本文件中删除标题?,python,text-files,Python,Text Files,我有大约2000个包含新闻文章摘要的文本文件,我想使用Python从所有有标题的文件(有些文件由于某种原因没有标题)中删除标题 下面是一个例子: Ad sales boost Time Warner profit Quarterly profits at US media giant TimeWarner jumped 76% to $1.13bn (£600m) for the three months to December, from $639m year-earlier.Its pr

我有大约2000个包含新闻文章摘要的文本文件,我想使用Python从所有有标题的文件(有些文件由于某种原因没有标题)中删除标题

下面是一个例子:

Ad sales boost Time Warner profit 

Quarterly profits at US media giant TimeWarner jumped 76% to $1.13bn (£600m) for the three months to December, from $639m year-earlier.Its profits were buoyed by one-off gains which offset a profit dip at Warner Bros, and less users for AOL.It lost 464,000 subscribers in the fourth quarter profits were lower than in the preceding three quarters.However, the company said AOL's underlying profit before exceptional items rose 8% on the back of stronger internet advertising revenues.Time Warner's fourth quarter profits were slightly better than analysts' expectations.For the full-year, TimeWarner posted a profit of $3.36bn, up 27% from its 2003 performance, while revenues grew 6.4% to $42.09bn.For 2005, TimeWarner is projecting operating earnings growth of around 5%, and also expects higher revenue and wider profit margins.
我的问题是如何删除“广告销售提振时代华纳利润”这句话

编辑:我基本上想在换行之前删除所有内容


TIA。

这将在第一次换行之前删除所有内容(
'\n\n'

试试这个: 它将文本拆分为换行符“\n\n”之前的所有内容,并仅选择最后一个元素(正文)


如果文本中没有换行符,也可以这样做,因为您可能知道,您无法读取和写入文件。-因此,这种情况下的解决方案是将行读取到变量;修改并重新写入文件

lines = []

# open the text file in read mode and readlines (returns a list of lines)
with open('textfile.txt', 'r') as file:
    lines = file.readlines()

# open the text file in write mode and write lines
with open('textfile.txt', 'w') as file:
    # if the number of lines is bigger than 1 (assumption) write summary else write all lines
    file.writelines(lines[2:] if len(lines) > 1 else lines)
以上是一个简单的例子,说明你如何实现你的目标尽管请记住可能存在边缘情况。

如果(如您所说)只是简单地删除第一行,那么当后面跟着
\n\n
时,您可以使用如下简单的正则表达式:

import re

with open('testing.txt', 'r') as fin:
    doc = fin.read()

doc = re.sub(r'^.+?\n\n', '', doc)

没有标题的文件看起来像什么?i、 e.您的程序如何判断文件中的第一行文本是否是标题,而不是文章的第一段?是否只想删除换行前的所有内容?标题如下:“title\n\n body”。如果没有标题,它看起来就像一个字符串,即“Body”。您可能想编辑您的问题@DeviKrishnan并包括一个示例。可以“Body”包含“\n\n”?这一个太具体了,您给了人们鱼竿,并教他们如何钓鱼。-精确解与上述结果接近;一些琐碎的想法会帮助他/她学习。你所谓的换行实际上是两个连续的换行,结果是一个空行。我同意你的看法。我使用的是问题中的语言。另外,保存到相同的文件名可以保证警告,如果没有备份,原始数据将丢失。保存到text-with-no-title.txt将是一个可行的替代方案。虽然有效,但在我看来,正则表达式似乎是解决此问题的一个过度手段。正则表达式速度很快,专为这种简单的文本匹配/替换问题而设计。这是一行代码,完全符合OP的要求。几乎不算“过火”。只有合理的答案,即使
doc='\n\n'.join(doc.split('\n\n')[1:])
也是一条单行线。不需要正则表达式。我喜欢在那里巧妙地使用
split
/
join
。也许你应该加上这个作为回答。我认为这是一个可靠和有效的替代我自己的。然而,我确实认为它的可读性稍差。很明显正则表达式在做什么(“替换模式OP”描述为“无”),尽管我对
join
/
split
很熟悉,但我还是要想一想它为什么会起作用。:-D我知道,阅读和理解简单的正则表达式很容易。但这只适用于像你我这样真正了解正则表达式的人。如果您愿意,可以将其作为备选方案纳入您的答案中。
line.split('\n\n', 1)[-1]
lines = []

# open the text file in read mode and readlines (returns a list of lines)
with open('textfile.txt', 'r') as file:
    lines = file.readlines()

# open the text file in write mode and write lines
with open('textfile.txt', 'w') as file:
    # if the number of lines is bigger than 1 (assumption) write summary else write all lines
    file.writelines(lines[2:] if len(lines) > 1 else lines)
import re

with open('testing.txt', 'r') as fin:
    doc = fin.read()

doc = re.sub(r'^.+?\n\n', '', doc)