2017-09-03 6 views
0

私はtxtファイルに複数のものを書き込もうとしていますが、何らかの理由で新しい行に各エントリを入れることができません。私は別の場所に '\ n'を置くことに戸惑っているが、結果は同じである。以下は、使用されているコードは次のとおりです。Python 3.X txtファイルへの書き込みは新しい行を作成しません

from collections import Counter 

File_1 = open('path1', 'r') 
wordCounter = Counter(File_1.read().lower().replace('<p>','').replace('<p><b>','').replace('</p>','').replace('</b>','').replace('.','').replace("'",'').replace('"','').replace('<i>','').replace('</i>','').replace(',','').replace('(','').replace('-','').replace(')','').replace('<b>','').replace(';','').split()) 
with open('path2','w') as File_2: 
    File_2.write('{:3} ==> {:15}'.format('Word','Count')) 
    File_2.write('-' * 18) 
    for (word,occurrence) in wordCounter.most_common(): 
     File_2.write('{:3} ==> {:15}'.format(word,occurrence)) 
File_1.close() 
File_2.close() 

は、多くの呼び出しを置き換える無視してください、私はそれをソートすることができます前に、クリーニングが必要グラグラテキストファイルで働いています。私は、各エントリがそうのように表示したい:私はおそらくここに新人のミスを作っていますように感じる

Word ==> Count ------------------Entry 1 ==> # of entriesEntry 2 ==> # of entries, etc. 

が、取得する簡単な方法があります:

Word ==> Count 
Eighteen dashes 
Entry 1 ==> # of entries 
Entry 2 ==> # of entries 
etc. 

を私が取得し終わるとは何ですか各エントリを新しい行に書き込むためのファイル?ご協力いただきましてありがとうございます。

+4

あなたは間違っていました。それは '/ n'ではなく' \ n'です –

答えて

1

前述のように、スラッシュ(/)ではなく、バックスラ(\)を使用します。

これはあなたの固定コードです:

from collections import Counter 
File_1 = open('path1', 'r') 
wordCounter = Counter(File_1.read().lower().replace('<p>','').replace('<p><b>','').replace('</p>','').replace('</b>','').replace('.','').replace("'",'').replace('"','').replace('<i>','').replace('</i>','').replace(',','').replace('(','').replace('-','').replace(')','').replace('<b>','').replace(';','').split()) 
with open('path2','w') as File_2: 
    File_2.write('{:3} ==> {:15}\n'.format('Word','Count')) 
    File_2.write(str('-' * 18)+'\n') 
    for (word,occurrence) in wordCounter.most_common(): 
     File_2.write('{:3} ==> {:15}\n'.format(word,occurrence)) 

File_1.close() 

あなた

+0

これは解決策でした。私は間違った場所に\ nを置いていました。どうもありがとうございます。 –

2

のためにあなたがにリダイレクトしてprint()機能を使用することができないこととするのであなたはまた、ファイル2のクローズを追加する必要はありませんファイル。 close()への呼び出しを心配する必要はありません:

from collections import Counter 


with open('path1', 'r') as file_1: 
    wordCounter = Counter(file_1.read().lower() 
          .replace('<p>','').replace('<p><b>','') 
          .replace('</p>','').replace('</b>','') 
          .replace('.','').replace("'",'') 
          .replace('"','').replace('<i>','') 
          .replace('</i>','').replace(',','') 
          .replace('(','').replace('-','') 
          .replace(')','').replace('<b>','') 
          .replace(';','').split()) 

with open('path2','w') as file_2: 
    print('{:3} ==> {:15}'.format('Word','Count'), file=file_2) 
    print('-' * 18, file=file_2) 
    for word, occurrence in wordCounter.most_common(): 
     print('{:3} ==> {:15}'.format(word,occurrence), file=file_2) 

は、私はまた、あなたがPEP 8 — the Style Guide for Python Code従うことをお勧めします、あなたが命名規則を持っている

また、ファイルを開くためにwith statementを使用することをお勧めします説明した。

注:のPython 2とprint()機能を使用するには、スクリプトの先頭に__future__ディレクティブを使用することができます。

from __future__ import print_function 
+0

それは彼の質問に答えません。彼は改行を追加したいです –

+0

情報ありがとう、私はスタイルガイドを見て、私が改善できるところを見てみましょう。 –

+0

@PokestarFan: 'print()'関数は( 'end =" "'を指定しない限り)改行を追加します。 –

関連する問題