如何将文件逐行读取到列表中? [英] How to read a file line-by-line into a list?
本文介绍了如何将文件逐行读取到列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Python中读取文件的每一行并将每一行作为元素存储在列表中?
How do I read every line of a file in Python and store each line as an element in a list?
我想逐行读取文件,并将每行追加到列表的末尾.
I want to read the file line by line and append each line to the end of the list.
推荐答案
with open(filename) as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
这篇关于如何将文件逐行读取到列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文