Home > Enterprise >  PrettyTable for loop is telling me TypeError: PrettyTable.add_row() takes 2 positional arguments but
PrettyTable for loop is telling me TypeError: PrettyTable.add_row() takes 2 positional arguments but

Time:03-17

I have been trying to figure out what the issue is and can't seem to figure it out.

valueRange = [j.value for i in copyRange for j in i]
vrCounter = 0
vrStep = 7
x.field_names = ["Jurisdiction","SPOC-Name", "Lines of Business","Market Area", "Consultant Personal Lines", "Consultant Business Lines", " ROC-Name"]

for i in range(0,len(valueRange)):
    x.add_row(valueRange[i], valueRange[i 1], valueRange[i 2], valueRange[i 3], valueRange[i 4], valueRange[i 5], valueRange[i 6])
print(x)

I ran the code without the x.add_row() function and it printed the values correctly. The valueRange list is just a bunch of keywords that match the field_names.

CodePudding user response:

As shown in the docs, add_row() takes a list of values as an argument.

Enclose all the values in a list and it must work.

x.add_row([valueRange[i], valueRange[i 1], valueRange[i 2], valueRange[i 3], valueRange[i 4], valueRange[i 5], valueRange[i 6]])

CodePudding user response:

Turn out the issue was the for i in range(0,len(valueRange)): portion. So it wasn't possible to answer this question without complete information about that valueRange list. This list contained 28 (0-27) string values. The loop was stepping through the 0-27 range 1 at a time while trying to apply an i number value to each one. This would quickly go out of bounds unless the step size matches the valueRange list.

The final solution looks like:

for i in range(0,len(valueRange),7):
    x.add_row(valueRange[i:i 7])
  • Related