python去除excel空行,Python-Excel:查找列中的第一个空行

博主在尝试用Python编写一个游戏并记录玩家得分时遇到问题。得分通过全局变量'points'跟踪,然后试图使用xlrd、xlwt和xlutils库将分数写入Excel工作簿。然而,他们遇到了不同模块之间对象不兼容的问题。解决方案是利用xlutils.copy的copy函数创建工作簿的副本,然后在副本上进行写操作,最后保存更改。
摘要由CSDN通过智能技术生成

working from my last question I've managed to get a good chunk of the way to get my system finished. Of course I've run across a problem.

I basically have a program that plays a game. Every correct answer adds 10 to the global variable 'points'. Then I want to add 'points' into an excel spreadsheet.

This is where I get very stuck. I'm running XLRD-0.8.0, XLUTILS-1.4.1 and XLWT-0.7.5.

Of course I've looked up different things but they don't seem to work for me.

This is a simplified version of my code:

import pygame, pygame.font, pygame.event, string, xlwt, xlrd, xlutils, socket

points = 0

def Uploadpoints(wbname):

global points

wb = xlrd.open_workbook(wbname)

# CODE TO FIND FIRST EMPTY CELL IN COLUMN 1 GOES HERE

wb.write(row,0,points)

wb.save()

Uploadpoints('workbook1.xls')

I thought of doing something like this but I'm not sure how I would go about it so I post pseudo-code.

Define worksheet: ws = 'sheet 1' [Done]

Define column: col = 0 [column A] [Done]

Search for first row in col that is empty: ??? [Not Done]

Define first row that is empty as row: row =????? [Not Done]

Any help would be greatly appreciated. Thanks in advance.

解决方案

My answer to this question a few days ago is very relelvant.

Basically whats going wrong is that xlrd and xlwt are different modules with different objects. The object you read in with xlrd.open_workbook() IS NOT the same object which xlwt knows how to write to

To get around this, theres the copy function in xlutils.

from xlutils.copy import copy

import os

wb = xlrd.open_workbook(name)

# Code to find the last open cell

wb = copy(wb) #This is the important line!

sheet = wb.get_sheet(num) #num is the index of the sheet you want to edit

sheet.write(whatever you want to write)

wb.save(name)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值