未毕业小白 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 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口