如何使用文本文件的每一行中的元素生成sublists
?
脚本
lines = tuple (open ('result.log', 'r'))
result.log
('value1', 'value2', 'value3', None)
('value1', 'value2', 'value3', None)
GENERATE OUTPUT
['value1', 'value2', 'value3', None]
['value1', 'value2', 'value3', None]
期望的输出:
[['value1'], ['value2'], ['value3'], None]
[['value1'], ['value2'], ['value3'], None]
答案 0 :(得分:1)
您可以使用列表理解
lines = tuple ({'value1', 'value2', 'value3', None})
[[l] if l else l for l in lines]
输出:
[None, ['value2'], ['value1'], ['value3']]