未毕业小白 2019-10-30 09:16 采纳率: 0%
浏览 197
已采纳

请问这段代码和我预期的结果不太一样,求大神帮忙看一下,谢谢。哪里的问题?

主要想实现分离excel数据,将数据按每500条分成一个sheet。但每次分离出来的数据虽然是500条,但第二个sheet第一个元素不是从第501开始,而是第50个开始的。详情看图

import os
import xlrd
import xlwt
from pip._vendor.distlib.compat import raw_input

# limit = raw_input('input limit number:')
limit = 500
readbook=r'D:\a\a.csv'
savebook=r'D:\b\bb.csv'
# readbook = raw_input('input excel read path:')
#
# savebook = raw_input('input excel save path:')

if limit == '':
    limit = 50  # 默认按100条拆分
print ('拆分数量: ' + str(limit))

limit = int(limit)

# '/Users/huqiang/Desktop/shoplist.xls'

data = xlrd.open_workbook(readbook)
# 获取sheet
table = data.sheets()[0]
# 行数
nrows = table.nrows
print(nrows)
# 列数
ncols = table.ncols
if nrows % limit != 0:
    sheets = (nrows // limit)+1
else:
    sheets=nrows // limit
# sheets = (nrows // limit)+1
print(sheets)
# print str(nrows) + '   ' + str(ncols)

# print table10.cell(nrows - 1, ncols - 1).value

workbook = xlwt.Workbook(encoding='ascii')

for i in range(0, int(sheets)):
    print('**',i)
    worksheet = workbook.add_sheet(str(i))
    for row in range(0, limit+1):![图片说明](https://img-ask.csdn.net/upload/201910/30/1572398082_764150.jpg)

        # print(row)
        row_content = table.row_values(row + (i*50))
        for col in range(0, ncols):
                worksheet.write(row, col, row_content[col])
workbook.save(savebook)

原始数据分离出的表1
g)

  • 写回答

1条回答 默认 最新

  • 二心TOT 2019-10-30 15:36
    关注

    就在你的代码基础上改了,写代码要把临界条件考虑完全

    # -*- coding: UTF-8 -*-
    import os
    import xlrd
    import xlwt
    from pip._vendor.distlib.compat import raw_input
    
    # limit = raw_input('input limit number:')
    limit = 500
    readbook=r'D:\a\a.csv'
    savebook=r'D:\b\bb.csv'
    # readbook = raw_input('input excel read path:')
    #
    # savebook = raw_input('input excel save path:')
    
    if limit == '':
        limit = 50  # 默认按100条拆分
    print ('拆分数量: ' + str(limit))
    
    limit = int(limit)
    
    # '/Users/huqiang/Desktop/shoplist.xls'
    
    data = xlrd.open_workbook(readbook)
    # 获取sheet
    table = data.sheets()[0]
    # 行数
    nrows = table.nrows
    print(nrows)
    # 列数
    ncols = table.ncols
    if nrows % limit != 0:
        sheets = (nrows // limit)+1
    else:
        sheets=nrows // limit
    # sheets = (nrows // limit)+1
    print('sheets:',sheets)
    # print str(nrows) + '   ' + str(ncols)
    
    # print table10.cell(nrows - 1, ncols - 1).value
    
    workbook = xlwt.Workbook(encoding='ascii')
    
    for i in range(0, int(sheets)):
        print('**',i)
        worksheet = workbook.add_sheet(str(i))
        if sheets-1 == i:
            for row in range(i*500, nrows):
                # print(row)
                row_content = table.row_values(row)
                for col in range(0, ncols):
                        worksheet.write(row-i*500, col, row_content[col])
        else:
            for row in range(0, limit):
                # print(row)
                row_content = table.row_values(row + (i*500))
                for col in range(0, ncols):
                        worksheet.write(row, col, row_content[col])
    
    workbook.save(savebook)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站