question

LiGavin-4784 avatar image
0 Votes"
LiGavin-4784 asked EvanChatter-2929 published

Azure Function list operation show index out of range

I'm writing some script on Azure function with python, which want to handle the csv file which was upload to blob storage.

The coding I tested pass in local, but it got error on Azure function.

Result: Failure Exception: IndexError: list index out of range Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 355, in handle_invocation_request call_result = await self.loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(self.args, *self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 542, in _run_sync_func return func(**params) File "/home/site/wwwroot/csvhandler/init.py", line 28, in main text1=list1[0]

Not quite sure why it said list index out of range Stack, the list index should begin from 0..


input string

ab,cd,ef\r\n12,34,56\r\nff,gg,ee\r\n

 import logging
 import azure.functions as func
 import pandas as pd
 import numpy as np
    
 list1=[]
    
 def main(inputblob: func.InputStream, outputblob: func.Out[str]) -> None:
     logging.info('Python Queue trigger function processed %s', inputblob.name)
            str1 = inputblob.read().decode('utf-8-sig')
     str1 = str1.split('\\r\\n')   #split the string in list, str1="ab,cd,ef\r\n12,34,56\r\nff,gg,ee\r\n"
     str1 = str1[0:-1]
     for i in range(0,len(str1)):    #split the list which can be written into pandas DataFrame
         str2=str1[i].split(',')
         list1.append(str2)
     print(str1)
        
     text1=list1[0]   #verify if the element correct
     text2=list1[1]
     text3=list1[2]
     text4=list1[3]
        
     df = pd.DataFrame(list1[1:], columns=list1[0])  #create the dataframe
        
     outstr=df.to_csv(index=False)   #transfer the output to string
     outputblob.set(outstr)          #output the string in the blob

In the other hand, would anyone suggest if we can explore the edited dataframe as csv to blob storage? I can't find much info here.



azure-functions
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LiGavin-4784 Thank you for the question. We will review and update accordingly!

0 Votes 0 ·
JayaC-MSFT avatar image
0 Votes"
JayaC-MSFT answered LiGavin-4784 edited

@LiGavin-4784 As mentioned in the thread : https://stackoverflow.com/questions/65196733/azure-function-list-operation-show-index-out-of-range/65203511, did you try to declare the list variable as global? Does that help in this case?

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi JayaC,

I modified the code, error POP UP the same.



Result: Failure Exception: IndexError: list index out of range Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 355, in handleinvocation_request call_result = await self.loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(self.args, *self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 542, in run_sync_func return func(**params) File "/home/site/wwwroot/csvhandler/init.py", line 22, in main text1=list1[0]







0 Votes 0 ·
     import logging
     import azure.functions as func
     import pandas as pd
     import numpy as np
        
     list1=[]
     text1 =[]
     text2 =[]
     def main(inputblob: func.InputStream, outputblob: func.Out[str]) -> None:
         logging.info('Python Queue trigger function processed %s', inputblob.name)
         str1 = inputblob.read().decode('utf-8-sig')
         str1 = str1.split('\\r\\n')
         str1 = str1[0:-1]
         global list1
         global text1
         global text2
         for i in range(0,len(str1)):
             str2=str1[i].split(',')
             list1.append(str2)
        
         print(str1)
         text1=list1[0]
         text2=list1[1]
        
         outputblob.set(str1)
0 Votes 0 ·
EvanChatter-2929 avatar image
0 Votes"
EvanChatter-2929 answered EvanChatter-2929 published

The IndexError is raised when attempting to retrieve an index from a sequence (e.g. list, tuple), and the index isn’t found in the sequence. The Python documentation defines when this exception is raised:

Raised when a sequence subscript is out of range. (Source)

Here’s an Python Split() example that raises the IndexError:

 data = "one%two%three%four%five"
 numbers = data.split('%')

The list numbers has 5 elements, and the indexing starts with 0, so, the last element will have index 4. If you try to subscript with an index higher than 4, the Python Interpreter will raise an IndexError since there is no element at such index.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.