python - Save line in file to list -


file = input('name: ')  open(file) infile:     line in infile:         name in infile:             name             print(name[line]) 

so if user pass file of vertical list of sentences, how save each sentence own list?

sample input:

'hi' 'hello' 'cat' 'dog' 

output:

['hi'] ['hello'] , on... 

>>> [line.split() line in open('file.txt')] [['hi'], ['hello'], ['cat'], ['dog']] 

or, if want more careful making sure file closed:

>>> open('file.txt') f: ...    [line.split() line in f] ...  [['hi'], ['hello'], ['cat'], ['dog']] 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -