2

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()
James Z
  • 12,209
  • 10
  • 24
  • 44
Pragati
  • 35
  • 4

1 Answers1

0

You can't write to an element that does not yet exist. listb=[] creates an empty list, so there is no element with index 0. You may append items like this: listb.append(foo).

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 one thing, and one thing exactly - and the workflow itself makes sure that all prerequisites are met. If not, you can throw and catch error messages, for example in another workflow, and ask users for input. If that logic is part of your script, this will be much harder.

Here's a very simple example:

control flow in UiPath

Wolfgang Radl
  • 2,319
  • 2
  • 17
  • 22
  • Thank you for your response. I want to check if the string contains specific keywords using python code. I have implemented (mz01arg.contains), but a lot of nested if's will delay the process on UIPath, hence thought of a python workaround. – Pragati Dec 19 '18 at 03:18