簡體   English   中英

如何在 python 中的一個 function 中使用兩個枚舉語句?

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

我試圖將兩個語句放入同一個 function 中。 這是將 csv 文件列表(所有不同的長度、頁眉、頁腳和列)導入到一個 Excel 工作表以導入數據庫的長查詢的一部分。 我想設置可以調用的函數來簡化流程。

現在,如果我運行以下代碼,它可以工作,我可以使用兩個參數: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)

然而; 如果我將它放入 function 中,那么我會收到兩條不同的錯誤消息,具體取決於我編寫 function 的方式。 對於第一個 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)

對於這個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)

這是我用於測試的數據的簡化版本:

在此處輸入圖像描述

不要使用被函數改變的全局變量。 而是讓 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)

不要使用兩個循環。 第一個是讀取所有文件,所以第二個循環沒有讀取任何內容。 您可以使用file.seek(0)修復此問題以返回到開頭,但在這種情況下不需要 - 只需在一個循環中測試這兩個條件。

您還應該使用參數和返回值而不是全局變量。

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