Azure Function list operation show index out of range

Li, Gavin 1 Reputation point
2020-12-08T10:19:12.847+00:00

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
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,194 questions
{count} votes

2 answers

Sort by: Most helpful
  1. JayaC-MSFT 5,531 Reputation points
    2020-12-09T11:13:44.487+00:00

    @Li, Gavin 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. Evan Chatter 16 Reputation points
    2021-08-09T06:22:12.517+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.