V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MyLeoWind
V2EX  ›  Python

逐行读取文本时哪种方法更好?

  •  
  •   MyLeoWind · 2016-06-05 16:55:40 +08:00 · 3881 次点击
    这是一个创建于 2882 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我知道逐行读取文本有两种方法: for line in f 以及 for line in f.readlines()。那么这两个方法哪个更好呢?为什么要设计 readlines()这个方法?
    10 条回复    2016-06-06 09:02:04 +08:00
    congeec
        1
    congeec  
       2016-06-05 17:06:58 +08:00
    用 for line in f, readlines()会把文件里的内容都读进内存
    https://stackoverflow.com/questions/17246260/python-readlines-usage-and-efficient-practice-for-reading
    21grams
        2
    21grams  
       2016-06-05 17:11:06 +08:00 via Android
    大文件用第一个,原因显而易见
    luofeiyu
        3
    luofeiyu  
       2016-06-05 17:29:52 +08:00
    内存受不了。
    lukertty
        4
    lukertty  
       2016-06-05 18:57:03 +08:00
    后者快一点
    loading
        5
    loading  
       2016-06-05 19:21:57 +08:00 via Android
    读一个大文件,自己对一下内存就知道了
    billlee
        6
    billlee  
       2016-06-05 20:24:18 +08:00
    方法一叫做逐行读取文本文件
    方法二叫做逐个读取列表元素
    OnTheRoad
        7
    OnTheRoad  
       2016-06-05 23:00:38 +08:00
    第二种方法读取大文件时内存遭不住。
    还是第一种方法最 Pythonic 。
    参见(stackoverflow)[http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python]
    lll9p
        8
    lll9p  
       2016-06-05 23:18:59 +08:00
    用 linecache 怎么样?
    jamesfjx
        9
    jamesfjx  
       2016-06-06 06:21:36 +08:00 via iPad
    还可以配合 yield 命令

    https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/

    最后一个例子
    practicer
        10
    practicer  
       2016-06-06 09:02:04 +08:00
    for line in f 语句将 file 对象转换成 iterable object ,既然是可迭代对象,一次只加载一个 item ,解释器不会将所有 items 放进内存,因此节省了资源。在 python 2.3 以前,要用 f.xreadlines()方法读大文件,它和 xrange 的作用一样,都是处理 iter(object),但在 2.3 后,官方明确用 for line in f 读取大文件。

    for line in f.readlines() 语句和第一个类似,但是它先执行 f.readlines(),直接把 file 对象中所有的 line items 列表存进内存,在它们之上进行循环读取。

    因此,读取大文件时用第一个语句,一般小文件这两个都可以。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   958 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:18 · PVG 04:18 · LAX 13:18 · JFK 16:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.