Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Key Value Pair output
#8
Each row in your actual query has 7 elements, while in your sample data had just 2. In the sample it was easy to asign each of these elements to different [meaningful] variable name. In the real query you can do the same - unpack each row into 7 different variables. Or you can do something different.

Let's focus just on line 23 and how you can replace it (any of the following would do):

json_rows = [dict(zip(('UPC', 'ItemFriendlyNames', 'ItemName', 'Brand', 'Model', 'Retailer', 'ItemID'),
                      (str(upc), item_friendly_name, item_name, brand, model, retailer, item_id)))
                    for upc, item_friendly_name, item_name, brand, model, retailer, item_id in rows]
json_rows = [dict(zip(('UPC', 'ItemFriendlyNames', 'ItemName', 'Brand', 'Model', 'Retailer', 'ItemID'), map(str, row))) for row in rows]
json_rows = [dict(zip(('UPC', 'ItemFriendlyNames', 'ItemName', 'Brand', 'Model', 'Retailer', 'ItemID'), (str(key), *values))) for key, *values in rows]
there are also other ways to do that... E.g. using map will cast to str all elements in the row. if there are some values that are not str and you need to keep them then you need to use something different

some more thoughts.
1. Instead of using cursor.fetchall() you can iterate directly over the cursor
2. instead of converting upc to str in the list comprehension (the line 23) you can cast it to string in the sql statement directly

"SELECT distinctrow CAST(UPC AS CHAR), ItemFriendlyNames, ItemName, Brand, Model, Retailer, ItemID FROM my.view order by UPC, Model, Retailer"
Also note that on line 32 you need to dump json_rows, not just rows
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Key Value Pair output - by UtiliseIT - May-02-2019, 05:57 AM
RE: Key Value Pair output - by buran - May-02-2019, 06:06 AM
RE: Key Value Pair output - by UtiliseIT - May-02-2019, 06:45 AM
RE: Key Value Pair output - by buran - May-02-2019, 06:59 AM
RE: Key Value Pair output - by UtiliseIT - May-02-2019, 07:00 AM
RE: Key Value Pair output - by buran - May-02-2019, 07:02 AM
RE: Key Value Pair output - by UtiliseIT - May-03-2019, 04:41 AM
RE: Key Value Pair output - by buran - May-03-2019, 07:06 AM
RE: Key Value Pair output - by UtiliseIT - May-03-2019, 07:20 AM
RE: Key Value Pair output - by buran - May-03-2019, 07:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting the maximum value:key pair from a dictionary sean1 2 1,546 Jan-17-2022, 01:04 PM
Last Post: DeaD_EyE
  How to extract specific key value pair from string? aditi06 0 2,661 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Auto re-pair / re-sync Controller via Script? User3000 2 2,494 Nov-30-2020, 11:42 AM
Last Post: User3000
  team pair issue jk91 29 8,894 Mar-03-2020, 06:15 PM
Last Post: jefsummers
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 7,135 May-29-2019, 02:27 AM
Last Post: heiner55
  Parsing Text file having repeated value key pair using python manussnair 3 3,430 Aug-04-2018, 11:48 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020