How to write lines in python to a list
This question already has an answer here:
Open the file using open()
and convert into a list like so:
with open("myfile.txt") as myfile:
int_list = [int(l.strip()) for l in myfile]
This iterates over every line, strips the n
at the end, converts into an int
, and puts it all into a list.
上一篇: 在Python2.7中导入一个文本文件
下一篇: 如何在Python中将行写入列表