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

针对这段 代码 如何进一步提高文件处理效率呢 最好是大幅度提高运行效率

  •  
  •   linzhi · Apr 20, 2014 · 4462 views
    This topic created in 4395 days ago, the information mentioned may be changed or developed.
    self.union_items_new_content = {}
    for line in self.union_items_new_fd.readlines():
    line = line.strip()

    if (line == "" or cmp(line[0], "#") == 0):
    continue

    list = line.split('\t')
    assert(list[0] != "")
    goodsid = int(list[0].strip())

    self.union_items_new_content[goodsid] = list
    5 replies    1970-01-01 08:00:00 +08:00
    linzhi
        1
    linzhi  
    OP
       Apr 20, 2014
    缩进 没处理好。。囧
    halfcrazy
        2
    halfcrazy  
       Apr 20, 2014
    lines=[line.strip().split("\t") for line in open('file') if line !="" and line[0]!="#"]
    linzhi
        3
    linzhi  
    OP
       Apr 20, 2014
    @halfcrazy 这样。。。。好像没法取到每行的第一个值 作为字典的key
    halfcrazy
        4
    halfcrazy  
       Apr 20, 2014
    @linzhi map(func,lines)
    func是添加key-value的函数
    gfreezy
        5
    gfreezy  
       Apr 21, 2014
    `for line in self.union_items_new_fd.readlines():`是一次读取全部的行吧。

    `for line in self.union_items_new_fd:`
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2349 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 105ms · UTC 11:24 · PVG 19:24 · LAX 04:24 · JFK 07:24
    ♥ Do have faith in what you're doing.