您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-如何将文件逐行读取到列表中?

Python-如何将文件逐行读取到列表中?

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] 

参见输入和输出

with open('filename') as f:
    lines = f.readlines()

或者去掉换行符:

with open('filename') as f:
    lines = [line.rstrip() for line in f]
python 2022/1/1 18:24:51 有409人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶