Search code examples
pythonpandasasync-awaitazure-functionsazure-blob-trigger

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?


Solution

  • 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 = pd.DataFrame()
    
      if myblob.name.__contains__("Sach"):
          ...
          Sach = read_excel_files("x", "Sach.xlsx")
          ...
      else:
          Sach = pd.DataFrame()
    
      if not Deb.empty and not Sach.empty:
          Konten = pd.merge(Sach, Deb, how="outer")