Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Excel文件中将正则表达式匹配元组迭代到行中时出现问题_Python 3.x_Loops_Openpyxl - Fatal编程技术网

Python 3.x 在Excel文件中将正则表达式匹配元组迭代到行中时出现问题

Python 3.x 在Excel文件中将正则表达式匹配元组迭代到行中时出现问题,python-3.x,loops,openpyxl,Python 3.x,Loops,Openpyxl,在研究了堆栈溢出和web之后,我找不到一个解决问题的方法,因此我在这里提出这个问题。我的目标是使用openpyxl将每个正则表达式匹配的5部分元组的结果逐个迭代到Excel行中(每组匹配一行;列保持不变)。摘录的代码如下: match = re.findall(pattern, inputfile.read()) if match: for tuple in match: sheet.cell(row=2, column=1).value = tuple[0]

在研究了堆栈溢出和web之后,我找不到一个解决问题的方法,因此我在这里提出这个问题。我的目标是使用openpyxl将每个正则表达式匹配的5部分元组的结果逐个迭代到Excel行中(每组匹配一行;列保持不变)。摘录的代码如下:

match = re.findall(pattern, inputfile.read())
if match:
    for tuple in match:
        sheet.cell(row=2, column=1).value = tuple[0]
        sheet.cell(row=2, column=2).value = tuple[1]
        sheet.cell(row=2, column=3).value = tuple[2]
        sheet.cell(row=2, column=4).value = tuple[3]
        sheet.cell(row=2, column=5).value = tuple[4]
然而,我遇到的问题是,我找不到一种方式进入下一场比赛的下一排。我的代码所做的只是编写第2行,然后用每个新的匹配项重写第2行(它从第2行开始,因为第1行是标题)。我尝试使用算术运算符(如+1)将行向前移动以获得新的匹配项,但这所做的只是将写入/覆盖过程向下移动1

如何修复这段代码,以便将每个新匹配项(包含5部分元组)插入到新行中,并且不会覆盖以前插入的匹配项?谢谢大家!


另外,我对堆栈溢出还不熟悉,因此如果我问的问题与正常情况不符,请让我知道(+我可以解决这些问题的方法)

您需要将count变量定义为全局变量

row = 2
match = re.findall(pattern, inputfile.read())
if match:
    for tuple in match:
        sheet.cell(row=row, column=1).value = tuple[0]
        sheet.cell(row=row, column=2).value = tuple[1]
        sheet.cell(row=row, column=3).value = tuple[2]
        sheet.cell(row=row, column=4).value = tuple[3]
        sheet.cell(row=row, column=5).value = tuple[4]
        row += 1

# now row equals to 2 + len(match)
match_2 = re.findall(pattern_2, inputfile.read())
if match_2:
    for tuple in match_2:
        sheet.cell(row=row, column=1).value = tuple[0]
        sheet.cell(row=row, column=2).value = tuple[1]
        sheet.cell(row=row, column=3).value = tuple[2]
        sheet.cell(row=row, column=4).value = tuple[3]
        sheet.cell(row=row, column=5).value = tuple[4]
        row += 1
这将起作用,但是有很多重复的代码

import itertools


def insert(patterns, inputfile, sheet, row=2):
    """insert matches into worksheet 

    :param patterns: pattern list e.g. [r'\d+', r'[a-z]*']
    :param inputfile: file to match
    :param sheet: worksheet
    :param row: start row
    :return:
    """
    match = itertools.chain(filter(lambda x: x, map(lambda x: re.findall(re.compile(x), inputfile.read()), patterns)))
    for r in match:
        for column in range(5):
            sheet(row=row, column=column+1).value = r[column]
            row += 1 

您需要将count变量定义为全局变量

row = 2
match = re.findall(pattern, inputfile.read())
if match:
    for tuple in match:
        sheet.cell(row=row, column=1).value = tuple[0]
        sheet.cell(row=row, column=2).value = tuple[1]
        sheet.cell(row=row, column=3).value = tuple[2]
        sheet.cell(row=row, column=4).value = tuple[3]
        sheet.cell(row=row, column=5).value = tuple[4]
        row += 1

# now row equals to 2 + len(match)
match_2 = re.findall(pattern_2, inputfile.read())
if match_2:
    for tuple in match_2:
        sheet.cell(row=row, column=1).value = tuple[0]
        sheet.cell(row=row, column=2).value = tuple[1]
        sheet.cell(row=row, column=3).value = tuple[2]
        sheet.cell(row=row, column=4).value = tuple[3]
        sheet.cell(row=row, column=5).value = tuple[4]
        row += 1
这将起作用,但是有很多重复的代码

import itertools


def insert(patterns, inputfile, sheet, row=2):
    """insert matches into worksheet 

    :param patterns: pattern list e.g. [r'\d+', r'[a-z]*']
    :param inputfile: file to match
    :param sheet: worksheet
    :param row: start row
    :return:
    """
    match = itertools.chain(filter(lambda x: x, map(lambda x: re.findall(re.compile(x), inputfile.read()), patterns)))
    for r in match:
        for column in range(5):
            sheet(row=row, column=column+1).value = r[column]
            row += 1 

openpyxl默认在当前
max\u行下方添加行。注意:这可能不是包含值的单元格的最后一行

以下内容适用于您:

match = re.findall(pattern, inputfile.read())
if match:
    for tuple in match:
        ws.append(tuple)

openpyxl默认在当前
max\u行下方添加行。注意:这可能不是包含值的单元格的最后一行

以下内容适用于您:

match = re.findall(pattern, inputfile.read())
if match:
    for tuple in match:
        ws.append(tuple)

谢谢你们两位伟大的回答;他们都工作!然而,在aishenghuomeidaoli的第一个解决方案和Charletchlark的解决方案之间,哪一个是最好的实现方案,特别是如果输入文件可以变成数千个匹配项那么大?在内存管理和其他因素方面是否有更好的选择,或者两者完全相同,因此可以有效地使用其中一种?有没有可能在异常情况下发生故障?谢谢谢谢你们两位伟大的回答;他们都工作!然而,在aishenghuomeidaoli的第一个解决方案和Charletchlark的解决方案之间,哪一个是最好的实现方案,特别是如果输入文件可以变成数千个匹配项那么大?在内存管理和其他因素方面是否有更好的选择,或者两者完全相同,因此可以有效地使用其中一种?有没有可能在异常情况下发生故障?谢谢谢谢你们两位伟大的回答;他们都工作!然而,在aishenghuomeidaoli的第一个解决方案和Charletchlark的解决方案之间,哪一个是最好的实现方案,特别是如果输入文件可以变成数千个匹配项那么大?在内存管理和其他因素方面是否有更好的选择,或者两者完全相同,因此可以有效地使用其中一种?有没有可能在异常情况下发生故障?谢谢没有区别,但我的建议也适用于
只写
模式,而另一种模式则不行。我明白了。非常感谢你的帮助!谢谢你们两位伟大的回答;他们都工作!然而,在aishenghuomeidaoli的第一个解决方案和Charletchlark的解决方案之间,哪一个是最好的实现方案,特别是如果输入文件可以变成数千个匹配项那么大?在内存管理和其他因素方面是否有更好的选择,或者两者完全相同,因此可以有效地使用其中一种?有没有可能在异常情况下发生故障?谢谢没有区别,但我的建议也适用于
只写
模式,而另一种模式则不行。我明白了。非常感谢你的帮助!