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

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

  •  
  •   linzhi · 2014-04-20 11:43:55 +08:00 · 3660 次点击
    这是一个创建于 3658 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    1970-01-01 08:00:00 +08:00
    linzhi
        1
    linzhi  
    OP
       2014-04-20 11:50:48 +08:00
    缩进 没处理好。。囧
    halfcrazy
        2
    halfcrazy  
       2014-04-20 11:57:30 +08:00
    lines=[line.strip().split("\t") for line in open('file') if line !="" and line[0]!="#"]
    linzhi
        3
    linzhi  
    OP
       2014-04-20 12:53:22 +08:00
    @halfcrazy 这样。。。。好像没法取到每行的第一个值 作为字典的key
    halfcrazy
        4
    halfcrazy  
       2014-04-20 16:20:59 +08:00
    @linzhi map(func,lines)
    func是添加key-value的函数
    gfreezy
        5
    gfreezy  
       2014-04-21 14:02:32 +08:00
    `for line in self.union_items_new_fd.readlines():`是一次读取全部的行吧。

    `for line in self.union_items_new_fd:`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5671 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 02:29 · PVG 10:29 · LAX 19:29 · JFK 22:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.