Python fetchall return true and false


Python fetchall return true and false



I have a python script that makes a fetchall():


connection = pymssql.connect(host='host', user='user', password='pw', database='database', as_dict=True)

cursor = connection.cursor()

cursor.execute("EXEC SPname")
data=cursor.fetchall()



My problem is that the cursor.fetchall() is returning True or Falses (and others columns with others values example above) but in the database the value is a bit 1 or 0 and when exported to CSV it puts the True or False and I want 1 or 0.



Sample data returned in SQL:


ID Price Price2 Price3 Active Opened Sent Enable
1 12234.09 1111.09 3444.36 1 1 1 0




2 Answers
2



You can use int()


int()



Ex:


print(int(True))
print(int(False))



Output:


1
0





Thank you for your answer and help. The problem is that the SP returns multiple columns, in this case 88, and I dont want to explicitly designates them because it has a lot of columns and I want something dynamic because if I alter the SP to return one more column I don't want to alter the script to add the column. The objective is next do the this: a= csv.writer(fp, delimiter=';') for line in data: a.writerows(line)
– peixinho3
Jul 3 at 9:53



a= csv.writer(fp, delimiter=';') for line in data: a.writerows(line)





You do not need to alter the SP, instead modify the bool value right before writing to the csv file.
– Rakesh
Jul 3 at 9:55





Correct but that implies that i make: a= csv.writer(fp, delimiter=';') for line in data: line["mycolumn1"] = int(line["mycolumn1"]) line["mycolumn2"] = int(line["mycolumn2"]) line["mycolumn3"] = int(line["mycolumn3"]) a.writerows(line) And this take a lots of time and if I had a new bit value in the SP I have to add another condition in the script
– peixinho3
Jul 3 at 10:14



a= csv.writer(fp, delimiter=';') for line in data: line["mycolumn1"] = int(line["mycolumn1"]) line["mycolumn2"] = int(line["mycolumn2"]) line["mycolumn3"] = int(line["mycolumn3"]) a.writerows(line)





Can you post a sample of data?
– Rakesh
Jul 3 at 10:22


data





Question edited with sample.
– peixinho3
Jul 3 at 10:35



fetchall returns a list of tuples or dictionaries. The easiest way to convert them to integers is by mapping the int method to that list:


fetchall


int


data_in_integer_form = map(int, data)





Thank you for your help but the data returned is not only booleans it's also decimal, strings, etc..
– peixinho3
Jul 3 at 10:21





Well, that's a different question than the one stated in the first place. You said the query was returning True and False, not decimals and strings.
– Poshi
Jul 3 at 10:27






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages