2

PRAWを使用して、特定のsubredditのすべての時間の上位25を取得しようとしています:

import praw
subreddit = 'gamedeals'
r = praw.Reddit(user_agent='getting top 25 of all time by /u/sqrg')
submissions = r.get_subreddit(subreddit).get_top_from_all(limit=25)
titlesFile = open("text.txt", 'w')
for s in submissions:
    titlesFile.write(s.title.encode('utf-8', 'replace') + '\n')
titlesFile.close()

次のエラーが表示されます。

UnicodeEncodeError: 'ascii' コーデックは位置 63 の文字 u'\xa3' をエンコードできません: 序数が範囲内にありません(128)

そこで、for ループ内の行を次のように変更しました。

titlesFile.write(s.title.encode('utf-8', 'replace') + '\n')

それは動作しますが、text.txt ファイルでは&代わりに.txt を取得します&。文字列置換機能で変更できますが、正しいタイトルを直接書く方法はありますか? また、なぜencode()メソッドを使用する必要があったのですか?

4

1 に答える 1