2016-10-02 3 views
0

ウェブサイト(http://www.gfrvitale.altervista.org/index.php/autismo-in?format=feed&type=rss)からrssフィードを削っています。 私はすべてのフィードからテキストを抽出して整形するスクリプトを書きました。私の主な問題は、各項目の各テキストを別のファイルに保存することです。また、それぞれのファイルに適切なタイトルのexctractetを付ける必要があります。 私のコードは次のとおりです。複数の出力を複数のファイルに保存するにはどうすればいいですか?各ファイルのタイトルはPythonのオブジェクトとは異なりますか?

for item in myFeed["items"]: 
    time_structure=item["published_parsed"] 
    dt = datetime.fromtimestamp(mktime(time_structure)) 

    if dt>t: 

    link=item["link"]   
    response= requests.get(link) 
    doc=Document(response.text) 
    doc.summary(html_partial=False) 

    # extracting text 
    h = html2text.HTML2Text() 

    # converting 
    h.ignore_links = True #ignoro i link 
    h.skip_internal_links=True #ignoro i link esterni 
    h.inline_links=True 
    h.ignore_images=True #ignoro i link alle immagini 
    h.ignore_emphasis=True 
    h.ignore_anchors=True 
    h.ignore_tables=True 

    testo= h.handle(doc.summary()) #testo estratto 

    s = doc.title()+"."+" "+testo #contenuto da stampare nel file finale 

    tit=item["title"] 

    # save each file with it's proper title 
    with codecs.open("testo_%s", %tit "w", encoding="utf-8") as f: 
     f.write(s) 
     f.close() 

エラーは次のとおりです。

File "<ipython-input-57-cd683dec157f>", line 34 with codecs.open("testo_%s", %tit "w", encoding="utf-8") as f: 
           ^
SyntaxError: invalid syntax 

答えて

0

あなたは%tit

後にコンマを置く必要があるようになります。しかし

#save each file with it's proper title 
with codecs.open("testo_%s" %tit, "w", encoding="utf-8") as f: 
    f.write(s) 
    f.close() 

、あなたのファイルならば名前に無効な文字が含まれていると、エラーが返されます(つまり、[Errno 22]

あなたはこのコードを試すことができます。

... 
tit = item["title"] 
tit = tit.replace(' ', '').replace("'", "").replace('?', '') # Not the best way, but it could help for now (will be better to create a list of stop characters) 

with codecs.open("testo_%s" %tit, "w", encoding="utf-8") as f: 
    f.write(s) 
    f.close() 

他の方法をnltkを使用して:

from nltk.tokenize import RegexpTokenizer 
tokenizer = RegexpTokenizer(r'\w+') 
tit = item["title"] 
tit = tokenizer.tokenize(tit) 
tit = ''.join(tit) 
with codecs.open("testo_%s" %tit, "w", encoding="utf-8") as f: 
    f.write(s) 
    f.close() 
+0

、私はこのエラーを取得する:C:\ Anaconda2 \ libに\コーデック.pyc in open(ファイル名、モード、エンコーディング、エラー、バッファリング) 894#バイナリモードでのファイルの強制オープン 895 mode = mode + 'b' - > 896 file = __builtin __。openバッファリング) エンコードされていない場合は897、 898リターンファイル IOError:[Errno 22]無効なモード( 'wb')またはファイル名:u'testo_La Comunicazione Facilitata? Parliamone。 – CosimoCD

+0

コードが正しいです。カンマはターゲットではなく、 '%tit'の後にあります。それは別のエラーです。私が確認しておきます、チェックします。 – estebanpdl

+0

希望の出力は何ですか? (つまり、 '.csv'、' .txt') – estebanpdl

0

まず第一に、あなたはカンマを紛失し、それがないの前に%titた後でなければなりません。

第2に、使用しているwithの文が自動的に実行されるため、ファイルを閉じる必要はありません。コーデックはどこから来たのですか?私はとにかく、正しいwithの文は次のようになります....どこにもそれを見ることはありません。私はそれをしなかったが、それは動作しません

with open("testo_%s" %tit, "w", encoding="utf-8") as f: 
    f.write(s) 
+0

上記のコードを実行しましたが、エラーが発生しました。今、私はこれを徹底的に取り除いています:io.open( "testo _" + tit、 "w"、encoding = "utf-8")をf: f.write( – CosimoCD

+0

)あなたは一緒に作業するための何かを提供する必要があります...あなたが '' testo _ "+ tit'が動作するとは思っていないので(' 'testo_%s"%tit''を使用してください) – geo1230

+0

私は上記のコードを実行しましたが、それは私にエラーを与える。この関数は%titのような引数を受け付けません。今度は、私はこれをやっています:io.open( "testo _" + tit、 "w"、encoding = "utf-8")f: f.write(s)彼の正当なタイトルとそれよりも停止します。私はこの新しいエラーを取得します:IOError:[Errno 22]無効な引数:u'testo_La Comunicazione Facilitata? Parliamone ... ' – CosimoCD

関連する問題