How do I convert .txt file to a list in python? -
i have file called movies.txt , contains list of 1000 different movie titles. e.g
a nous la liberte (1932) schmidt (2002) absence of malice (1981) adam's rib (1949) .... ....
i extract these movie titles in .txt file , add them list in python program getting type error. here code.
note: "file name: "
input 'movies.txt'
.
file = open((input("file name: ")), "r") movies_list = file.readlines() movies_list = [movie.strip() movie in movies_list] file.close() print(movies_list[0,1])
this error get
traceback (most recent call last): file "/users/liamemery/pycharmprojects/assignmenttwo/questiontwo.py", line 40, in <module> load() file "/users/liamemery/pycharmprojects/assignmenttwo/questiontwo.py", line 6, in load print(movies_list[0,1]) typeerror: list indices must integers or slices, not tuple
your problem appears displaying results. when selecting list, can either select index, e.g.:
print movies_list[0]
or range, e.g.:
print movies_list[0:3] print movies_list[3:-1]
or can print entire list:
print movies_list
you can't use commas in square brackets lists.
Comments
Post a Comment