[Tutor] Debugging a sort error.
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Jan 13 03:55:24 EST 2019
On 13/01/2019 02:16, mhysnm1964 at gmail.com wrote:
> Issue, following error is generated after trying to sort a list of strings.
>
> description.sort()
> TypeError: unorderable types: float() < str()
Please send the complete error message not just the
last line summary. There is a lot of potentially
useful information in the missing lines.
> ['Description', 'EFTPOS WOOLWORTHS 1294 ", "withdrawal Hudson
> street 3219"]
That doesn't look right. The quotes are all mixed
up and miss-matched. Can you send an actual example?
> There is over 2000 such entries. This used to work and now doesn't. Only
> change to the code was modifying the data variable from a list to a
> dictionary.
The data variable appears to only have one entry, namely "Amount".
Is that what you intended?
Below is the full code:
>
>
>
> import openpyxl
>
> from openpyxl.utils import get_column_letter
>
> from more_itertools import unique_everseen
>
>
>
> # Loding the workbook and names for the sheets.
>
> wb = openpyxl.load_workbook("test.xlsx")
>
> wss = wb.get_sheet_names()
>
>
>
> description = [] # all the descriptions
>
> data = {}
>
>
>
> for ws_name in wss:
>
> ws = wb.get_sheet_by_name(ws_name)
>
> for column in ws.columns:
>
> col_data = [] # column list
>
> for cell in column:
>
> value = cell.value
>
> col_data.append(value)
>
> # end for cell loop
>
> if ws_name == "WestPac":
>
> if col_data[0] == 'Credit Amount':
>
> num = 1
>
> while num <=
> (len(col_data) - 1):
>
> value =
> col_data[num]
>
> if
> value:
>
>
> data['Amount'][num] = value
>
> else:
>
>
> data['Amount'][num] = data['Amount'][num] * -1
>
> num +=
> 1
>
> # end while num
>
> break
>
> # end if
>
> # end if
>
> if col_data[0] in data:
>
>
> data[col_data[0]].extend(col_data[1:-1])
>
> else:
>
> data[col_data[0]] = col_data
>
> # end for column
>
> #end for ws_name
>
>
>
> description = data['Description']
You try to assign data["Description"] but you never populated
that in the code above. You only ever used data["Amount"].
> for i in description:
> if not str(i):
> print "not a string")
I'm not sure what you think that's doing but the print should
give a syntax error if you are using Python 3.5 as you claim.
You need parens around it. Can you cut n paste the actual code?
Also, it is unlikely to ever print anything because most
things can be converted to a string.
However, given that description is supposed to contain
lists of values I suspect you need another inner loop
to convert the list contents.
I think you wanted something like:
for lst in description:
for i in lst:
if type(i) is not str:
print(i, "is not a string")
> description.sort()
>
> I am suspecting it is something to do with the data but cannot track down
> the cause. Any suggestions on how to debug this?
Send the full error and some genuine data and try
the modified type test loop above.
Oh, and using smaller indentation will make it more
readable too. 3-4 spaces is the usual size.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list