Jan-21-2023, 12:43 AM
(This post was last modified: Jan-21-2023, 12:43 AM by herobpv.
Edit Reason: typo
)
I need to write a subset of data into a table using pymssql library. I am able to prepare the data I want but stuck on how to write this data into a table.
I need to write this result to a table which has id, country_name, zip
using pymssql library in python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import json data = { "demographic" : [ { "id" : 1 , "country" : { "code" : "AU" , "name" : "Australia" }, "state" : { "name" : "New South Wales" }, "location" : { "time_zone" : { "name" : "(UTC+10:00) Canberra, Melbourne, Sydney" , "standard_name" : "AUS Eastern Standard Time" , "symbol" : "AUS Eastern Standard Time" } }, "address_info" : { "address_1" : "", "address_2" : "", "city" : "", "zip_code" : "" } }, { "id" : 2 , "country" : { "code" : "AU" , "name" : "Australia" }, "state" : { "name" : "New South Wales" }, "location" : { "time_zone" : { "name" : "(UTC+10:00) Canberra, Melbourne, Sydney" , "standard_name" : "AUS Eastern Standard Time" , "symbol" : "AUS Eastern Standard Time" } }, "address_info" : { "address_1" : "", "address_2" : "", "city" : "", "zip_code" : "" } }, { "id" : 3 , "country" : { "code" : "US" , "name" : "United States" }, "state" : { "name" : "Illinois" }, "location" : { "time_zone" : { "name" : "(UTC-06:00) Central Time (US & Canada)" , "standard_name" : "Central Standard Time" , "symbol" : "Central Standard Time" } }, "address_info" : { "address_1" : "", "address_2" : "", "city" : "", "zip_code" : "60611" } } ] } |
1 |
result = [(d[ "id" ], d[ "country" ][ "name" ], d[ "address_info" ][ "zip_code" ]) for d in data[ "demographic" ] if d[ "country" ][ "code" ] = = "US" ] |
buran write Jan-20-2023, 09:31 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.