2016-11-17 10 views
-4

10-12行ごとにページ番号を持つテキスト文書を編集したい(PDFをテキストに変換し、ページの最後にページ番号を付ける)。私は、ページ番号50がある可能性があるので、テキストではないこれらの特定のページ番号整数を削除したいが、整数として50があることができる行でもあり得る。だから私はページ番号整数を持つ行だけを削除したい。テキストドキュメントのPythonで行を読み込んで行を削除するには?

例:

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からこれらのページ番号を削除したいが、これらの同じ番号がどこかのラインの間にそれが除去されていない必要があります表示されます。 pythonの使用は必須ではない場合

私のコード

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) 
+0

コードがありますか?ここで質問する前に、少なくとも自分で問題を解決しようとするべきです。削除したい番号が新しい行の間にあるように見えるので、正規表現を使うことができます(5番を除く)。 – user2393256

+0

あなたはこれまでに何を試しましたか?どこに問題がありますか? [ファイルを読む?](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)[タイプの検出?](http://stackoverflow.com/questions/2225038)/determine-a-python-object)を指定します。すでに努力していることを示してください... –

+0

すべてをメモリに読み込み、テキストを編集し、すべてのファイルに書き込みます。またはlin-by-lineを読んで、新しいファイルに書き込む行を決定してください。後で古いファイルを削除し、新しいファイルの名前を古い名前に変更します。 – furas

答えて

1

あなたはgrep -v '^[0-9][\s]*' test.txtを使用することができます。

[email protected]:~/$ 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. 
関連する問題