with open('filename') as f:
for line in f:
line = line.rstrip().split(', ') #split the line at ', '
for item in line[1:]: #iterate over slice of the list returned in the previous step
print line[0], item #print the first item, and the item from the loop.
输出:
说明:
在这里,我们首先剥离尾随的空格,然后在
', '
处拆分行。此步骤返回一个列表。你知道吗使用切片,我们可以从秒开始获取所有项目:
现在,我们可以遍历这个切片列表并打印
lis[0]
和循环期间获取的项。你知道吗相关问题 更多 >
编程相关推荐