現在、テキストファイルを入力し、各単語を分離してリストに整理しようとしています。
私が抱えている現在の問題は、テキスト ファイルからカンマとピリオドを取り除くことです。
私のコードは以下の通りです:
#Process a '*.txt' file.
def Process():
name = input("What is the name of the file you would like to read from? ")
file = open( name , "r" )
text = [word for line in file for word in line.lower().split()]
word = word.replace(",", "")
word = word.replace(".", "")
print(text)
私が現在得ている出力は次のとおりです。
['this', 'is', 'the', 'first', 'line', 'of', 'the', 'file.', 'this', 'is', 'the', 'second', 'line.']
ご覧のとおり、「ファイル」と「行」の語尾にはピリオドが付いています。
私が読んでいるテキストファイルは次のとおりです。
これはファイルの最初の行です。
これが2行目です。
前もって感謝します。