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
sjmcefc2
V2EX  ›  Python

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

  •  
  •   sjmcefc2 · 2018-05-20 17:02:58 +08:00 · 2347 次点击
    这是一个创建于 2138 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    7 条回复    2018-05-21 13:10:22 +08:00
    afpro
        1
    afpro  
       2018-05-21 02:06:50 +08:00 via Android
    不能
    sjmcefc2
        2
    sjmcefc2  
    OP
       2018-05-21 02:50:50 +08:00
    貌似还是只有重写文件这一个办法?
    cfwyy
        3
    cfwyy  
       2018-05-21 09:10:56 +08:00
    就为了删除文本某一行?为何不直接用 sed?
    mypchas6fans
        4
    mypchas6fans  
       2018-05-21 10:17:14 +08:00
    sed/awk 教做人
    laike9m
        5
    laike9m  
       2018-05-21 11:05:35 +08:00
    mmap 可能不行,fileinput 是可以做到的,把 inplace=True 即可
    laike9m
        6
    laike9m  
       2018-05-21 11:08:27 +08:00
    不过 fileinput 好像还是得遍历一遍……效率也不高
    weyou
        7
    weyou  
       2018-05-21 13:10:22 +08:00
    从文件系统来看任何方法都不可避免的要重写文件。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5495 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 85ms · UTC 08:17 · PVG 16:17 · LAX 01:17 · JFK 04:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.