【发布时间】:2014-12-31 13:51:11
【问题描述】:
我正在尝试将行读入一个列表,其中该行上的每个单词都是不同的参数。例如,当我的文本文件包含:
Word1, Word2, Some different words,separated by comma,but no space
Word3, Word4, Some different words,separated by comma,but no space
我想要这样的列表:
['Word1', 'Word2', 'Some different words,separated by comma,but no space'],
['Word3', 'Word4', 'Some different words,separated by comma,but no space']
也许我什至可以得到这样的列表:
['Word1', 'Word2', 'Some different words','separated by comma', 'but no space']
到目前为止,当文本文件中有一行时,我已经通过将每个单词读入列表来完成这项工作。
list_words = f.read().split()
它给了我输出:
['Word1', 'Word2', 'Some different words,separated by comma,but no space']
当我有多行时,我该怎么做?另外,如果我以后想从两个列表中打印出第一个参数,我可以使用 list_words[0] 它会自动给我 'Word1' 和 'Word3' 吗?
我希望这个解释足够清楚。
【问题讨论】:
标签: python