我目前正在尝试从一个看起来像这样的文本文件中读取:
10/11/17, 10:00, 02:00, [mary, john, scott]
11/11/17, 12:00, 01:30, [ashley, john, sarah]
12/11/17, 12:00, 03:00, [steve, mick]
我希望程序将文件作为单个元素读入:
"10/11/17", "10:00", "02:00", [mary, john, scott]
"11/11/17", "12:00", "01:30", [ashley, john, sarah]
"12/11/17", "12:00", "03:00", [steve, mick]
我的问题是,当我尝试执行以下代码时,它会将所有内容输出为包含列表的字符串,我的目标是将3个字符串和一个列表传递给另一个函数,我想将它们分开“ ,“
infile = open('todo.txt').read().splitlines()
for line in infile:
print(line.split())
答案 0 :(得分:1)
首先,将线条分成四个部分,然后用列表中的最后一个元素做一些奇特的魔术:
for line in infile:
line_list = line.split(', ', maxsplit=3)
line_list[3] = line_list[3].strip("[]").split(', ')
答案 1 :(得分:-1)
添加','被分割() infile = open(' todo.txt')。read()。splitlines()
for infile: 打印(line.split('&#39))