如何在Python中读取行和删除行?

2024-06-09 07:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想编辑一个文本文档,在每10-12行的末尾有页码(将PDF转换成文本,在页面末尾有页码)。我想删除这些特定的页码整数不在文本中,因为可以有一个页码50,但也可以有一行,其中可以有50作为整数。所以我只想删除页码为整数的行。在

文本文档示例:

1 





militant Muslims use scriptures such as the 
Genesis story describing the destruction of 
Sodom and Gomorrah as justification (from Allah) 
for the hatred they vent on all things  non-
Muslim and especially on gay men.  

2 


A Word from the Author 

Today, in the 21st Century the majority of Muslims 
hold middle 

3 


Into The Darkness 


the driver assured the exhausted travelers who 
were dozing fitfully in the rear of the van, they

4 


down. It blocked the narrow road.  
Ali Azzizi was the other man accompanying 
the women. 
5 

我想删除这些页码从1-5,但如果这些相同的数字出现在任何地方之间的行,它不应该删除。在

我的代码

^{pr2}$

Tags: andoftheinfrom文本编辑on
1条回答
网友
1楼 · 发布于 2024-06-09 07:28:23

如果python的使用不是强制性的,那么可以使用grep -v '^[0-9][\s]*' test.txt。在

cristian@nb:~/$ grep -v '^[0-9][\s]*' test.txt 





militant Muslims use scriptures such as the 
Genesis story describing the destruction of 
Sodom and Gomorrah as justification (from Allah) 
for the hatred they vent on all things  non-
Muslim and especially on gay men.  



A Word from the Author 

Today, in the 21st Century the majority of Muslims 
hold middle 



Into The Darkness 


the driver assured the exhausted travelers who 
were dozing fitfully in the rear of the van, they



down. It blocked the narrow road.  
Ali Azzizi was the other man accompanying 
the women. 

相关问题 更多 >