简体   繁体   English

如何在 python 中的一个 function 中使用两个枚举语句?

[英]How to use two enumerate statements in one function in python?

I am trying to take two statements and put them into the same function.我试图将两个语句放入同一个 function 中。 This is part of a long query to import a list of csv files (all different lengths, headers, footers, and columns) into one Excel sheet for importing into a database.这是将 csv 文件列表(所有不同的长度、页眉、页脚和列)导入到一个 Excel 工作表以导入数据库的长查询的一部分。 I would like to set up functions that can be called to streamline the process.我想设置可以调用的函数来简化流程。

Right now, if I run the following code, it works and I can use the two parameters: beginFile and endFile to determine the start and end of the data for importing.现在,如果我运行以下代码,它可以工作,我可以使用两个参数:beginFile 和 endFile 来确定要导入的数据的开始和结束。

beginning = 'eventID'
ending = 'The Line Listing is wrong'

beginFile = 0
endFile = 0

with open("testbooklet.csv") as myFile:
    for num, line in enumerate(myFile, 1):
        if beginning in line:
            beginFile = num
            
with open("testbooklet.csv") as myFile:
    for num, line in enumerate(myFile, 1):
        if ending in line:
            endFile = num
            
print(beginFile,endFile)

However;然而; if I place this into a function, then I receive two different error messages, depending on how I write the function.如果我将它放入 function 中,那么我会收到两条不同的错误消息,具体取决于我编写 function 的方式。 For this first function, the error message is AttributeError: 'function' object has no attribute 'endFile'.对于第一个 function,错误消息是AttributeError: 'function' object has no attribute 'endFile'.

beginning = 'eventID'
ending = 'The Line Listing is wrong'

beginFile = 0
endFile = 0

# Define Function to find the first and last file lines
def fileinfo(file_name):
    global beginFile
    global endFile
    
    for num, line in enumerate(file_name, 1):
        if beginning in line:
            fileinfo.beginFile = num

# def endfileinfo(file_name):        
    for num, line in enumerate(file_name, 1):
        if ending in line:
            fileinfo.endFile = num

MyFile = open("testbooklet.csv")             
fileinfo(MyFile)
print(fileinfo.beginFile, fileinfo.endFile)

For this function, the error code is: NameError: name 'endFile' is not defined对于这个function,错误代码是: NameError: name 'endFile' is not defined

beginning = 'eventID'
ending = 'The Line Listing is wrong'

beginFile = 0
endFile = 0

def fileinfo(file_name):
    global beginFile
    
    for num, line in enumerate(file_name, 1):
        if beginning in line:
            beginFile = num
   
    global endFile
            
    for num, line in enumerate(file_name, 1):
        if ending in line:
            endFile = num

            
MyFile = open("testbooklet.csv")
fileinfo(MyFile)
print(beginFile)
print(endFile)

This is a simplistic version of the data that I'm using for testing:这是我用于测试的数据的简化版本:

在此处输入图像描述

Don't use global variables that get mutated by functions.不要使用被函数改变的全局变量。 Instead let the function return whatever you need, and get both informations in one sweep:而是让 function 返回您需要的任何内容,并在一次扫描中获取这两个信息:

def fileinfo(file):
    beginFile = None
    endFile = None
    for num, line in enumerate(file, 1):
        if beginning in line:
            beginFile = num
        if ending in line:
            endFile = num
            break  # No need to continue
    return beginFile, endFile  # return this information to caller


myFile = open("testbooklet.csv")             
beginFile, endFile = fileinfo(myFile)
print(beginFile, endFile)

Don't use two loops.不要使用两个循环。 The first one is reading all of the file, so there's nothing for the second loop to read.第一个是读取所有文件,所以第二个循环没有读取任何内容。 You could fix this with file.seek(0) to return to the beginning, but there's no need in this case -- just test both conditions in a single loop.您可以使用file.seek(0)修复此问题以返回到开头,但在这种情况下不需要 - 只需在一个循环中测试这两个条件。

You also should use parameters and return values rather than global variables.您还应该使用参数和返回值而不是全局变量。

def fileinfo(file, beginning, ending):
    beginFile = 0
    endFile = 0

    for num, line in enumerate(file, 1):
        if beginning in line:
            beginFile = num
        if ending in line:
            endFile = num

    return beginFile, endFile

with open("testbooklet.csv") as MyFile:
    begin, end = fileinfo(MyFile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM