Azure功能列表操作显示索引超出范围



我正在用python在Azure函数上编写一些脚本,希望处理上传到blob存储的csv文件。我测试的编码在本地通过了,但在Azure函数上出现了错误。

结果:失败异常:IndexError:列表索引超出范围Stack:File/azure函数host/workers/python/3.8/LINUX/X64/azure_functions_workers/dispatcher.py";,第355行,在_handle__invocation_request call_result=等待self中_loop.run_in_executor(文件"/usr/local/lib/python3.8/concurrentfutures/thread.py",第57行,运行中结果=self.fn(*self.args,**self.kwargs(文件"/azure函数host/workers/python/3.8/LINUX/X64/azure_functions_workers/dispatcher.py";,第542行,在__run_sync_func return func(**params(File"home/site/wwwwroot/csvhandler/init.py";,第28行,在主文本1=list1[0]中

不太确定为什么它说列表索引超出范围堆栈,列表索引应该从0开始。

输入字符串

ab,cd,ef\r\n1234,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,efrn12,34,56rnff,gg,eern"
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

另一方面,有人建议我们是否可以将编辑后的数据帧探索为csv到blob存储?我在这里找不到太多信息

看起来您在主函数的范围之外定义了变量list1,这意味着它是一个全局变量。对于Azure函数,我认为您必须在主函数中显式声明全局变量才能编辑它们。因此,当您附加csv文件中的元素时,您的列表可能不会更新,这将导致列表索引错误。应该有两种方法来解决这个问题(可能还有其他方法(:

  1. list1定义为主函数内部的局部变量
def main(inputblob: func.InputStream, outputblob: func.Out[str]) -> None:
logging.info('Python Queue trigger function processed %s', inputblob.name)
list1 = []

str1 = inputblob.read().decode('utf-8-sig')
  1. list1声明为全局变量,以便在主函数内部使用。(请参阅此处的Azure函数文档(
list1 = []
def main(inputblob: func.InputStream, outputblob: func.Out[str]) -> None:
logging.info('Python Queue trigger function processed %s', inputblob.name)

global list1

str1 = inputblob.read().decode('utf-8-sig')

最新更新