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

Postby TheFunk » Thu Sep 12, 2013 3:59 pm

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

Postby Mekire » Thu Sep 12, 2013 4:09 pm

Your rowvar[0] here is still a tuple.

Code: Select all
from decimal import Decimal

rowvar = [(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.403293117838986
5.40

-Mek
User avatar
Mekire
 
Posts: 984
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Formatting Data from a List

Postby TheFunk » Thu Sep 12, 2013 5:08 pm

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: Google [Bot] and 2 guests