python逐行读取文本文件

python逐行读取文本文件,python,printing,split,anaconda,filereader,Python,Printing,Split,Anaconda,Filereader,我希望我的代码从文本文件中读取并将数据填入列表中 我想要达到的代码: dataset = [['a', 'b', 'c'], ['b', 'c'], ['a', 'b', 'c'], ['d'], ['b', 'c']] 我已经尝试了以下代码: dataset = open(filename).read().split('\n') for items in dataset: print(i

我希望我的代码从文本文件中读取并将数据填入列表中

我想要达到的代码:

dataset = [['a', 'b', 'c'],
           ['b', 'c'],
           ['a', 'b', 'c'],
           ['d'],
           ['b', 'c']]
我已经尝试了以下代码:

dataset = open(filename).read().split('\n')
for items in dataset:
        print(items)

我得到了包含空格的列表,那么我如何解决这个问题呢?
谢谢

此脚本将文件加载到
数据集
列表中:

dataset = []
with open(filename, 'r') as f_in:
    for items in f_in:
        dataset.append(items.split())

print(dataset)
印刷品:

[['a', 'b', 'c'], ['b', 'c'], ['a', 'b', 'c'], ['d'], ['b', 'c']]

您可以逐行阅读,然后按单词拆分每行:

dataset = []
with open(filename, 'r') as fp:
    for line in fp.lines():
        dataset.append(line.split())

您必须对输入进行两次分区:一次是为了获得单独的输入行;第二次
将每一行拆分为单独的字母。