file = input('Name: ')
with open(file) as infile:
for line in infile:
for name in infile:
name
print(name[line])
因此,如果用户要传递垂直句子列表的文件,我该如何将每个句子保存到自己的列表中?
输入样例:
'hi'
'hello'
'cat'
'dog'
输出:
['hi']
['hello']
and so on...
>>> [line.split() for line in open('File.txt')]
[['hi'], ['hello'], ['cat'], ['dog']]
或者,如果我们要更仔细地确保关闭文件,请执行以下操作:
>>> with open('File.txt') as f:
... [line.split() for line in f]
...
[['hi'], ['hello'], ['cat'], ['dog']]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句