推荐学习书目
› 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
Gimini
V2EX  ›  Python

Python 新手求问一个 Django 处理 excel 数据的问题

  •  1
     
  •   Gimini · Mar 7, 2018 · 3786 views
    This topic created in 3135 days ago, the information mentioned may be changed or developed.
    
    files = request.FILES.get('files')
                data = xlrd.open_workbook(filename=None, file_contents=files.read())
                table = data.sheet_by_index(0)
                nrows = table.nrows
                ncols = table.ncols
                for i in range(1, nrows):
                    row = table.row_values(i)
                    for j in range(1, ncols):
                        print row[j]
    
    
    

    现在我有个 model 字段名分别是 n1~n40, 怎么按列循环每一行的第 n 个数据插入 nn 个字段里, 比如 表格第 2 列加入字段 n1。

    刚学 python 感觉有点绕晕了

    4 replies  •  2018-03-07 10:59:28 +08:00
    Gimini
        1
    Gimini  
    OP
       Mar 7, 2018
    MyModel.objects.create(n1=row[1]....)
    这种笨办法穷举会遇到
    IndexError: list index out of range
    vZexc0m
        2
    vZexc0m  
       Mar 7, 2018   ❤️ 1
    试试 openpyxl,仔细看文档
    RyougiShiki
        3
    RyougiShiki  
       Mar 7, 2018   ❤️ 1
    table.write(i, j, row[j])
    Gimini
        4
    Gimini  
    OP
       Mar 7, 2018
    @vZexc0m #2
    @RyougiShiki #3
    非常感谢已经解决
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   744 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 21:24 · PVG 05:24 · LAX 14:24 · JFK 17:24
    ♥ Do have faith in what you're doing.