Formatting Data from a List

This is the place for queries that don't fit in any of the other categories.

Formatting Data from a List

Hi all,

I've finally started pulling useful data from SQL Server and have been able to update booleans stored in SQL database tables. I can even (from Python) manipulate number values stored in the tables. My one problem lies in formatting data returned from the database. I'm a Linux nerd, so sed and cut, and awk typically do this kind of thing for me.

pyodbc is returning rows, which behave identically to lists. For example:

print rowvar returns
Code: Select all
`[(Decimal('5.403293117838986'), )]`

Whereas print rowvar[0] returns
Code: Select all
`(Decimal('5.397395667530735'), )`

I'd like to make that Decimal and those parentheses disappear. I can think of a few ways to do it, but what would be the simplest/best method? Everything I can think of involves either mountains of if statements, or turning the list into a string, then back into a list of characters, and then pulling a range.
TheFunk

Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Re: Formatting Data from a List

Your rowvar[0] here is still a tuple.

Code: Select all
`from decimal import Decimalrowvar = [(Decimal('5.403293117838986'), )]print(rowvar)print(rowvar[0])print(rowvar[0][0])print("{:.2f}".format(rowvar[0][0]))`
Code: Select all
`>>> [(Decimal('5.403293117838986'),)](Decimal('5.403293117838986'),)5.4032931178389865.40`

-Mek

Mekire

Posts: 1296
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: Formatting Data from a List

Oh! That makes much more sense! I just thought that the Decimal() being added was a mess-up of pyodbc. I should probably reread the pyodbc info page, and the Python tutorials for that matter, it's been too long. I just ordered a book on Python and Qt, so hopefully I can catch up that way. Thank you!

http://www.amazon.com/Programming-Python-Prentice-Software-Development/dp/0132354187/ref=cm_cr_pr_product_top#selectedObb=rbb_ubb_trigger
TheFunk

Posts: 27
Joined: Fri Aug 30, 2013 5:46 pm

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 7 guests