pythonでファイルからスペース以外の特殊文字を削除するには?
膨大な量のテキスト(一行ずつ)があり、特殊文字を除去したいが、文字列のスペースや構造は維持したい。
hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by@ an#other %million^ %%like $this.
は、次のようになります。
hello there A Z R T world welcome to python
this should be the next line followed by another million like this
9
3
このパターンは、
regex
でも使用できます。出力します。
編集します。
そうでなければ、最終行を
list
に格納することができます。出力します。
しかし、私は単純な正規表現を追加して、単語のない文字をすべて取り除きます。
特殊文字をNoneにマッピングする辞書の作成
辞書を使って翻訳テーブルを作る。 テキスト全体を変数に読み込んで、テキスト全体にstr.translateを使用します。