Each line an element in an array when reading text file in Python

This question already has an answer here:

  • In Python, how do I read a file line-by-line into a list? 35 answers

  • import io
    
    data = []
    with io.open('MasterList.txt', encoding='latin-1') as myfile:
        for i in myfile.readlines():
            data.append(i)
    

    Then you can loop data and compare it with the entered password . You can use:

    if password in data:
        print "Password found in data"
    
    链接地址: http://www.djcxy.com/p/42350.html

    上一篇: Python:将每行单词放入列表中

    下一篇: 在Python中读取文本文件时,每行都包含一个数组中的元素