在Python中通过Excel进行字符串/正则表达式搜索 [英] String/regex search over Excel in Python issue

查看:669
本文介绍了在Python中通过Excel进行字符串/正则表达式搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于SO来说我是新手,而对Python而言则相对较新,所以如果这是一个简单的修复程序或一个不合适的问题,我感到抱歉.

I'm a newb to SO, and relatively new to Python, so i'm sorry if this is a simple fix or an inappropriate question.

首先,我的程序通常可以运行,但是我正在尝试实现一些冗余/功能扩展以使其健壮.

Firstly, my program generally works, but i'm trying to implement some redundancy/catchalls for to make it robust.

程序查看excel文件的目录(和子目录),分别打开它们,搜索数据(在特定的工作表上),然后将其转储到csv中.由于每个搜索词实际上都是针对列的开头,因此涉及到循环,我希望在此之下有4个值.

The program looks over a directory (and sub-dirs) of excel files, opens them individually, scours for data (on a specific sheet), and dumps it out to a csv. There are loops involved as each search term is effectively for the head of a column, and i want 4 values beneath this.

我使用正则表达式定义搜索词.

I use regular expressions to define search terms.

我编写了一个函数来搜索excel工作表以查找与正则表达式的匹配项.该工作表中的单元格中包含字符串和其他格式类型,因此字符串的类型(查询).

I've written a function to search over the excel sheet for a match to a regular expression. The sheet has strings and other format-types within the cells, hence the type(query) for strings.

def SearchXLWithRe(regex)
    for i in range(1, Row_limit):         # row limit is defined by OpenPyXL module
        for j in range(1, Column_limit):    # same here for column limit
            query = ws.cell(row = i, column = j).value
            if type(query) == str:         # i only want to look at strings
                if regex.search(query):    # of the responses that are strings, i want to match to the regex
                    return [i,j]

此功能用于搜索在那里的字符串(迄今为止一直如此).我想在一些 excel文件中不包含我要搜索的词时添加冗余,但是其他人会(在一个空白单元格中,例如1000,1000或类似的东西,它可能会返回一些组合坐标) ).

This function works for searches on strings that are there (which has so far always been the case). I want to add redundancy for when some excel files wont contain terms I want to search for, but others will (it could just return some made up coordinates for a blank cell at eg. 1000,1000 or something).

我尝试放置else,但是由于它遍历excel文档并查找多个字符串,因此返回的所有内容均为None.

I have tried putting an else but as it's looping over an excel doc and finding multiple string, all this returns is a None.

我认为我有一个简单的逻辑问题,但我看不到;如果有人可以为我提供一些帮助,我们将不胜感激(并且非常热心!).

I think i have a simple logic problem, but I just can't see it; if anyone can offer me some pointers the help would be gratefully (and eagerly!) received.

我已审核过的问题(但我仍然迷路了):

Questions i've reviewed (but i'm still lost):

在Python我应该如何测试变量是否为None,True或False

推荐答案

def SearchXLWithRe(regex)
    for i in range(1, Row_limit):         # row limit is defined by OpenPyXL module
        for j in range(1, Column_limit):    # same here for column limit
            query = ws.cell(row = i, column = j).value
            if type(query) == str:         # i only want to look at strings
                if regex.search(query):    # of the responses that are strings, i want to match to the regex
                    return [i,j]
     return [x,y] #x,y are the dummy locations

仅在for循环之后返回,只有在未找到匹配项时才执行.

Just return after the for loops, it will only be executed if no match was found.

这篇关于在Python中通过Excel进行字符串/正则表达式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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