2016-09-03 12 views
0

私はhttpヘッダーを含むドキュメントのデータセットを持っています。私はこれらの文書を辿り、残りのテキストを残してこれらのヘッダーを削除したい。どうやってやるの?テキストファイルから定義済みのテキスト(httpヘッダー)を削除する方法

WARC/1.0 
WARC-Type: response 
WARC-Date: 2012-02-10T21:58:44Z 
WARC-TREC-ID: clueweb12-0000wb-76-38422 
WARC-IP-Address: 207.241.148.80 
WARC-Payload-Digest: sha1:W6JMWCNM43FDYNW466OADMH2KDGKJCGR 
WARC-Target-URI: http://someurl.http 
WARC-Record-ID: <urn:uuid:5a783f09-f0d8-4564-8f3a-c0d1ace7177b> 
Content-Type: application/http; msgtype=response 
Content-Length: 26043 

HTTP/1.1 200 OK 
Date: Fri, 10 Feb 2012 21:58:45 GMT 
Server: Apache 
Vary: * 
PRAGMA: no-cache 
P3P: CP="IDC DSP COR DEVa TAIa OUR BUS UNI" 
Cache-Control: max-age=-3600 
Expires: Fri, 10 Feb 2012 20:58:45 GMT 
Connection: close 
Content-Type: text/html 
+0

これらはHTTPヘッダーであり、HTMLではありません。 –

+0

はい、私は「headers」という単語を使用しています。私はメッセージを正しく伝えられなかったかもしれません。私はそれを改めた。 –

+0

この文書の入手方法を表示する必要があります。通常、HTTPヘッダーは応答の本体に含まれません。 –

答えて

1

これは、あなたが望むことをします。 元のファイルだけを残して、クリーンアップしたバージョンを新しいファイルに入れます。

datafile = 'test1.txt' 
outputfile = 'output.txt' 

with open(outputfile, encoding='utf-8', mode='w') as outfile: 
    with open(datafile, encoding='utf-8', mode='r') as infile: 
     foundhdrstart = False 

     for line in infile: 
      if line.strip() == 'WARC/1.0': 
       foundhdrstart = True 
      if foundhdrstart is False: 
       outfile.write(line) 
      if line.strip() == 'Content-Type: text/html': 
       foundhdrstart = False 
関連する問題