简体   繁体   中英

local variable '*' referenced before assignment while using 2 IFs

I'm trying to merge 2 Dataframes. Due to Blobtrigger i have to check which file is it that's being read. Also i used Async because it jumped from one line to another (Multithread) and right now python executes commands line by line, which makes it easier for me to navigate but if that's redundant pls do tell. When it get's to pd.merge i get this error:

local variable 'Deb' referenced before assignment

async def main(myblob: func.InputStream,  outputblob: func.Out[str]) -> None:
  if  myblob.name.__contains__("Deb"):
      logging.info("Deb was found")
      Deb = read_excel_files("x", "Deb.xlsx")
      logging.info("Starting cleaning Process")        
      .....
      logging.info("Cleaning Deb is finished")
  if myblob.name.__contains__("Sach"):
      logging.info("Sach was found")
      Sach = read_excel_files("x", "Sach.xlsx")
      logging.info("Starting cleaning Process")
      ........
      logging.info("Cleaning Sach is finished")
      Konten = pd.merge(Sach, Deb, how="outer")
      outputblob.set(Konten.to_string())
      logging.info("Konten is uploaded")
    

i thought the Variables that's been used in first IF can be Accessed in second IF. i have just Observed that after this line

Sach = read_excel_files("x", "Sach.xlsx")

Deb which has a value will be Unassigned. should i used.Copy instead?

Try using an else after the first if statement to assign a value to Deb , such as None , same for the second and Sach . Then move your merge under a 3rd if statement that checks for truthiness of both Deb and Sach before attempting to merge

Something like:

  if  myblob.name.__contains__("Deb"):
      ...
      Deb = read_excel_files("x", "Deb.xlsx")
      ...
  else:
      Deb = None

  if myblob.name.__contains__("Sach"):
      ...
      Sach = read_excel_files("x", "Sach.xlsx")
      ...
  else:
      Sach = None

  if Deb and Sach:
      Konten = pd.merge(Sach, Deb, how="outer")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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