Reading from a plain text file

后端 未结 4 882
天涯浪人
天涯浪人 2020-12-11 08:24

Say I have the following in a text file:

car
apple
bike
book

How can I read it and put them into a dictionary

4条回答
  •  离开以前
    2020-12-11 08:51

    words = []
    for word in open('words.txt'):
        words.append(word.rstrip('\n'))
    

    The words list will contain the words in your file. strip removes the newline characters.

提交回复
热议问题