Python: AttributeError: '_io.TextIOWrapper' object has no attribute 'split'

前端 未结 3 1529
清歌不尽
清歌不尽 2020-11-29 03:36

I have a textfile, let\'s call it goodlines.txt and I want to load it and make a list that contains each line in the text file.

I tried using the

3条回答
  •  渐次进展
    2020-11-29 04:06

    You're not reading the file content:

    my_file_contents = f.read()
    

    See the docs for further infos

    You could, without calling read() or readlines() loop over your file object:

    f = open('goodlines.txt')
    for line in f:
        print(line)
    

    If you want a list out of it (without \n as you asked)

    my_list = [line.rstrip('\n') for line in f]
    

提交回复
热议问题