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 · 2758 次点击
    这是一个创建于 2258 天前的主题,其中的信息可能已经有所发展或是发生改变。

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

    
    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   ·   实用小工具   ·   2759 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:10 · PVG 19:10 · LAX 04:10 · JFK 07:10
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com