Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in RPA by (12.7k points)

I am currently facing an error

list assignment index out of range

within the invoke Python scope. I am just trying to check if each of the variables contains any of the string mentioned in 'a'. If yes then add it as a row to the excel sheet.

import pandas as pd

import xlsxwriter

def excel_data(mz01arg,p028arg,p006arg,s007arg,mz01desc,p028desc,p006desc,s007desc):

    listb=[]

    a=['MZ01','P028','P006','S007']

    if any (x in mz01arg for x in a) is True:

        listb[0] = [mz01arg]

    else:

        listb[0] = []

    if any (x in p028arg for x in a ) is True:

        listb[1] = [p028arg]

    else:

        listb[1] =[]

    if any (x in p006arg for x in a) is True:

        listb[2]=[p006arg]

    else:

        listb[2] = []

    if any (x in s007arg for x in a) is True:

        listb[3]=[s007arg]

    else:

        listb[3]=[]

    df1 = pd.DataFrame({'SODA COUNT': listb})

    df2 = pd.DataFrame({'SODA RISK DESCRIPTION': [mz01desc,p028desc,p006desc,s007desc]})

    writer = pd.ExcelWriter(r"D:\Single_process_python\try_python.xlsx", engine='xlsxwriter')

    df3 = pd.concat([df1,df2],axis=1)

    df3.to_excel(writer,sheet_name='Sheet1', index=False)

    writer.save()

1 Answer

0 votes
by (29.5k points)
Hi, you can't write an element that does not exist at that point of time, what you can do is make a listb[]=0 which creates an an emplty list in which you can append items like listb.append(prakhy).  

However, since you mentioned UiPath - I would recommend checking variables and their values in the workflow instead of your Python script. This way your script does only one thing and the workflow itself makes sure that all prerequisites are met.

If they are not met then you can throw and catch error messages.

Browse Categories

...