Jak používat dva výčet výroků v jedné funkci v pythonu?

0

Otázka

Snažím se to brát dvě prohlášení, a dát je do stejné funkce. To je součástí dlouhé dotaz importovat seznam z csv souborů (všech různých délkách, záhlaví, zápatí a sloupcích) do jednoho listu aplikace Excel pro import do databáze. Chtěl bych nastavit funkce, které lze volat k zefektivnění procesu.

Teď, když jsem spustit následující kód, funguje to a můžu použít dva parametry: beginFile a endFile určit začátek a konec dat pro import.

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)

Nicméně, když jsem umístit do funkce, pak jsem přijímat dva různé chybové zprávy, v závislosti na tom, jak jsem se napsat funkci. Pro tento první funkce, chybová zpráva je 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)

Pro tuto funkci, kód chyby je: 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)

Je to zjednodušující verze data, která používám pro testování:

enter image description here

enumerate function python
2021-11-23 21:21:25
2

Nejlepší odpověď

2

Nepoužívejte globální proměnné, které se zmutoval do funkce. Místo toho nechť funkce vrátit, co potřebujete, a získat jak informace v jednom cyklu:

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)
2021-11-23 21:31:00
1

Nepoužívejte dvě smyčky. První z nich je čtení všech souborů, takže není nic pro druhé smyčky číst. Můžete to napravit s file.seek(0) pro návrat na začátek, ale není třeba v tomto případě-jen test, jak podmínky v jednom obraze.

Také byste měli používat parametry a návratové hodnoty, spíše než globální proměnné.

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)
2021-11-23 21:32:37

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................