Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中读取行和删除行?_Python_File - Fatal编程技术网

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

如何在Python中读取行和删除行?,python,file,Python,File,我想编辑一个文本文档,每10-12行末尾都有页码(将PDF转换为文本,并在页面末尾有页码)。我想删除这些特定的页码整数,不是在文本中,因为可以有页码50,也可以是一行,其中可以有50作为整数。所以我只想删除页码为整数的行 文本文档示例: 1 militant Muslims use scriptures such as the Genesis story describing the destruction of Sodom and Gomorrah as justificati

我想编辑一个文本文档,每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中删除这些页码,但如果这些相同的页码出现在行与行之间的任何位置,则不应删除

我的代码

filename = input('filname')
filedata = None

temp = 1

with open(filename, 'r', encoding="utf8") as file:
    filedata = file.read()
    filedata.join(line.strip() for line in file)
    rahul = '                                                                                                                                    '
    for line in file:
        if(line=='1'):
         filedata = filedata.replace(line, ' ')







with open(filename, 'w', encoding="utf8") as file:
  file.write(filedata)

如果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. 

你有密码吗?在这里提问之前,你至少应该试着自己解决你的问题。您可以尝试使用正则表达式,因为您要删除的数字似乎在新行之间(数字5除外…),到目前为止您尝试了什么?你遇到的问题在哪里。表明你已经付出了努力…将所有内容读入内存,编辑文本,将所有内容写入文件。或者逐行读lin并决定哪一行写入新文件。然后删除旧文件,并将新文件重命名为旧名称。SRY我忘记了请看我的编辑问题。我被这个问题弄糊涂了,因为它几乎是的复制+粘贴,除了另一个是Java。为什么你想用不同的语言做同样的事情?