无法从文件中创建逐行列表?

时间:2017-11-17 03:32:16

标签: python list file iteration

这就是我所拥有的(包括额外处理):

import string

def main():
    fileInput = open("A tisket a tasket.txt",'r')
    characters, words, lines = countEmUp(fileInput)
    printCounts(characters, words, lines)
    tokenList = splitToTokens(fileInput)
    print(tokenList)

def countEmUp(someFileInput):
    lines, words, chars = 0, 0, 0
    for line in someFileInput:
        chars += len(line)  # using += for nice and concise updates
        words += len(line.split())
        lines += 1
    return chars, words, lines

def printCounts(ch,wd,ln):
    print("Your file contains the following:")
    print("Characters:",ch)
    print("Words:",wd)
    print("Lines:",ln)

def splitToTokens(aFileInput):
    tokenList = []
    for line in aFileInput:
        tokenList.extend(line.split())
    return tokenList

  main()

问题从splitToTokens函数开始。

尝试做的是创建一个空列表,遍历我打开的文件,逐行读取,并将该行中的标记添加到我的tokenList中我可以稍后处理这些令牌。

当我在main()函数中打印tokenList时,它仍然是一个空列表。

我怀疑它可能与我的fileInput已被调用有关吗?我试图不要多次打开文件。

1 个答案:

答案 0 :(得分:4)

您正在尝试重新读取文件,您必须将光标移动到文件的开头:

file.seek(0)

将您的代码更改为:

import string

def main():
    fileInput = open("A tisket a tasket.txt",'r')    
    characters, words, lines = countEmUp(fileInput)
    fileInput.seek(0) #offset of 0
    printCounts(characters, words, lines)
    tokenList = splitToTokens(fileInput)
    print(tokenList)

def countEmUp(someFileInput):
    lines, words, chars = 0, 0, 0
    for line in someFileInput:
        chars += len(line)  # using += for nice and concise updates
        words += len(line.split())
        lines += 1
    return chars, words, lines

def printCounts(ch,wd,ln):
    print("Your file contains the following:")
    print("Characters:",ch)
    print("Words:",wd)
    print("Lines:",ln)

def splitToTokens(aFileInput):
    tokenList = []
    for line in aFileInput:
        tokenList.extend(line.split())
    return tokenList
main()