使用XLRD在Excel单元格中查找特定字符串 [英] Find specific String in Excel Cell using XLRD

查看:862
本文介绍了使用XLRD在Excel单元格中查找特定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的excel文件包含一些特定值,其中一个单元格包含一般描述。有时在描述中会出现一些特定关键字的警告信息(例如,高,低,增加,减少,关闭)。有没有办法在excel单元格中搜索这些特定单词(单词可能在一个句子中,例如意外的高音量),并获取单元格位置?



我尝试了以下代码,但不能正常工作。 (只是稍微更新,而不是知道单元格位置,我想复制没有警告消息关键字的文件)

  import glob 
import os
import shutil
import xlrd
os.chdir(C:/ Users / tsengineer / Desktop / New folder / Trial)
选项= raw_input(指定要复制的年份(例如2012)并按Enter)
location = raw_input(指定要复制的位置(例如C:\Users\tsengineer\Desktop)并按Enter )
notcopy =(增加,减少,高,低,关闭)
在glob.glob(*。xls)中的文件:
如果文件中的选择:
book = xlrd.open_workbook(file)
sheet = book.sheet_by_index(0)
for range in range(sheet.nrows):
for column in范围(sheet.ncols):
如果在sheet.cell(行,列)中为notcopy .value:
continue
else:
shutil.copy(文件,位置)
打印警察ying完成:


解决方案

我不是Python专家,但您似乎正在使用中的关键字



您正在检查是否有数组存在于单元格中,这不是你想要的。你想看看数组中的任何项目是否在单元格中。



你可能正在寻找更像:

 如果有(在sheet.cell(行,列)中的值)在$ notcopy中的值)
/ pre>

My excel files contains some specific values and one of the cells contains general description. Sometimes in the description there are some warning messages with certain keywords (e.g. high, low, increase, decrease, closure). Is there a way to search for these specific words in an excel cell (the words might be in a sentence;e.g. unexpected high volume) and get the cell location?

I tried the following code, but not working. (just minor update, instead of knowing the cell location, I want to copy the files which do not have the warning message keywords)

import glob
import os
import shutil
import xlrd
os.chdir("C:/Users/tsengineer/Desktop/New folder/Trial")
choice = raw_input("Specify the Year to Copy (e.g. 2012) and Press Enter ")
location = raw_input("Specify the location to Copy (e.g. C:\Users\tsengineer\Desktop) and Press Enter ")
notcopy = ("increase", "decrease", "high", "low", "Closure")
for file in glob.glob("*.xls"):
    if choice in file:
        book = xlrd.open_workbook(file)
        sheet = book.sheet_by_index(0)
        for row in range(sheet.nrows):
            for column in range(sheet.ncols):
                if notcopy in sheet.cell(row,column).value:  
                    continue
                else:
                    shutil.copy(file, location)
print "Copying Complete:"

解决方案

I'm not a Python expert, but it looks like you're using the keyword in incorrectly.

You're checking to see if an array exists in the cell value, which isn't what you want. You want to see if any of the items in the array are in the cell value.

You're probably looking for something more like:

if any(s in sheet.cell(row,column).value for s in notcopy):

这篇关于使用XLRD在Excel单元格中查找特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆