V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
sjmcefc2
V2EX  ›  Python

如何直接删除文本中某一行?

  •  
  •   sjmcefc2 · May 20, 2018 · 2826 views
    This topic created in 2912 days ago, the information mentioned may be changed or developed.

    想通过 mmap 读取文件中的行,然后直接读写该文本删除某些特定的行。 怎么实现呢?

    7 replies    2018-05-21 13:10:22 +08:00
    afpro
        1
    afpro  
       May 21, 2018 via Android
    不能
    sjmcefc2
        2
    sjmcefc2  
    OP
       May 21, 2018
    貌似还是只有重写文件这一个办法?
    cfwyy
        3
    cfwyy  
       May 21, 2018
    就为了删除文本某一行?为何不直接用 sed?
    mypchas6fans
        4
    mypchas6fans  
       May 21, 2018
    sed/awk 教做人
    laike9m
        5
    laike9m  
       May 21, 2018
    mmap 可能不行,fileinput 是可以做到的,把 inplace=True 即可
    laike9m
        6
    laike9m  
       May 21, 2018
    不过 fileinput 好像还是得遍历一遍……效率也不高
    weyou
        7
    weyou  
       May 21, 2018
    从文件系统来看任何方法都不可避免的要重写文件。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   874 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 21:38 · PVG 05:38 · LAX 14:38 · JFK 17:38
    ♥ Do have faith in what you're doing.