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

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

  •  1
     
  •   Gimini · 2018-03-07 08:31:35 +08:00 · 2740 次点击
    这是一个创建于 2236 天前的主题,其中的信息可能已经有所发展或是发生改变。
    
    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 条回复    2018-03-07 10:59:28 +08:00
    Gimini
        1
    Gimini  
    OP
       2018-03-07 08:47:02 +08:00
    MyModel.objects.create(n1=row[1]....)
    这种笨办法穷举会遇到
    IndexError: list index out of range
    vZexc0m
        2
    vZexc0m  
       2018-03-07 08:54:15 +08:00   ❤️ 1
    试试 openpyxl,仔细看文档
    RyougiShiki
        3
    RyougiShiki  
       2018-03-07 09:46:45 +08:00   ❤️ 1
    table.write(i, j, row[j])
    Gimini
        4
    Gimini  
    OP
       2018-03-07 10:59:28 +08:00
    @vZexc0m #2
    @RyougiShiki #3
    非常感谢已经解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2974 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 03:10 · PVG 11:10 · LAX 20:10 · JFK 23:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.