Python Source ?

Hi
I want to read this code and export to a excel file.
Is it possible ? Which node ? and how to do exactly
import pandas as pd

Create the data list with all rows as dictionaries

data = [
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “WONDER KIT”, “GOING”: “G”,
“POS”: 1, “JOCKEY”: “L Hewitson”, “TRAINER”: “K W Lui”, “DRAW”: 1, “TRACK”: “TURF B”,
“WT R”: 135, “DIST”: 1200, “HKR”: 80, “SP”: 6.3, “DBTN”: “0,75”, “TIME”: “69,67”,
“S1”: “23,84”, “S2”: “22,66”, “S3”: “23,17”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.13”, “3F 2F”: “11.53”, “2F 1F”: “11.51”, “1F 0”: “11.66”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “SUPER JOY N FUN”, “GOING”: “G”,
“POS”: 2, “JOCKEY”: “C L Chau”, “TRAINER”: “T P Yung”, “DRAW”: 3, “TRACK”: “TURF B”,
“WT R”: 121, “DIST”: 1200, “HKR”: 67, “SP”: 4.4, “DBTN”: “0,75”, “TIME”: “69,79”,
“S1”: “24,08”, “S2”: “22,66”, “S3”: “23,05”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.17”, “3F 2F”: “11.49”, “2F 1F”: “11.43”, “1F 0”: “11.62”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “GOLDEN RISE”, “GOING”: “G”,
“POS”: 3, “JOCKEY”: “A Atzeni”, “TRAINER”: “J Size”, “DRAW”: 2, “TRACK”: “TURF B”,
“WT R”: 118, “DIST”: 1200, “HKR”: 63, “SP”: 3.7, “DBTN”: “0,75”, “TIME”: “69,82”,
“S1”: “24,20”, “S2”: “22,70”, “S3”: “22,92”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.13”, “3F 2F”: “11.57”, “2F 1F”: “11.51”, “1F 0”: “11.41”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “SUGAR SUGAR”, “GOING”: “G”,
“POS”: 4, “JOCKEY”: “E C W Wong”, “TRAINER”: “C Fownes”, “DRAW”: 12, “TRACK”: “TURF B”,
“WT R”: 127, “DIST”: 1200, “HKR”: 79, “SP”: 31, “DBTN”: “1,25”, “TIME”: “69,87”,
“S1”: “23,72”, “S2”: “22,58”, “S3”: “23,57”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.09”, “3F 2F”: “11.49”, “2F 1F”: “11.67”, “1F 0”: “11.90”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “STORM RIDER”, “GOING”: “G”,
“POS”: 5, “JOCKEY”: “B Thompson”, “TRAINER”: “D A Hayes”, “DRAW”: 10, “TRACK”: “TURF B”,
“WT R”: 122, “DIST”: 1200, “HKR”: 67, “SP”: 19, “DBTN”: “1,75”, “TIME”: “69,94”,
“S1”: “24,72”, “S2”: “22,58”, “S3”: “22,64”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.25”, “3F 2F”: “11.33”, “2F 1F”: “11.35”, “1F 0”: “11.29”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “NORDIC DRAGON”, “GOING”: “G”,
“POS”: 6, “JOCKEY”: “A Hamelin”, “TRAINER”: “C S Shum”, “DRAW”: 9, “TRACK”: “TURF B”,
“WT R”: 125, “DIST”: 1200, “HKR”: 70, “SP”: 17, “DBTN”: “2”, “TIME”: “69,99”,
“S1”: “24,64”, “S2”: “22,46”, “S3”: “22,89”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.13”, “3F 2F”: “11.33”, “2F 1F”: “11.51”, “1F 0”: “11.38”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “REGAL GEM”, “GOING”: “G”,
“POS”: 7, “JOCKEY”: “K C Leung”, “TRAINER”: “F C Lor”, “DRAW”: 7, “TRACK”: “TURF B”,
“WT R”: 123, “DIST”: 1200, “HKR”: 68, “SP”: 11, “DBTN”: “2,25”, “TIME”: “70,03”,
“S1”: “24,08”, “S2”: “22,70”, “S3”: “23,25”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.17”, “3F 2F”: “11.53”, “2F 1F”: “11.63”, “1F 0”: “11.62”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “HAYDAY”, “GOING”: “G”,
“POS”: 8, “JOCKEY”: “M F ■■■■”, “TRAINER”: “P F Yiu”, “DRAW”: 5, “TRACK”: “TURF B”,
“WT R”: 116, “DIST”: 1200, “HKR”: 61, “SP”: 16, “DBTN”: “3”, “TIME”: “70,13”,
“S1”: “24,00”, “S2”: “22,66”, “S3”: “23,47”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.09”, “3F 2F”: “11.57”, “2F 1F”: “11.63”, “1F 0”: “11.84”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “HEALTHY HEALTHY”, “GOING”: “G”,
“POS”: 9, “JOCKEY”: “H Bowman”, “TRAINER”: “P C Ng”, “DRAW”: 8, “TRACK”: “TURF B”,
“WT R”: 129, “DIST”: 1200, “HKR”: 74, “SP”: 34, “DBTN”: “3,25”, “TIME”: “70,18”,
“S1”: “24,40”, “S2”: “22,58”, “S3”: “23,20”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.13”, “3F 2F”: “11.45”, “2F 1F”: “11.39”, “1F 0”: “11.81”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “GALLANT VALOUR”, “GOING”: “G”,
“POS”: 10, “JOCKEY”: “B Avdulla”, “TRAINER”: “J Richards”, “DRAW”: 4, “TRACK”: “TURF B”,
“WT R”: 128, “DIST”: 1200, “HKR”: 73, “SP”: 7.3, “DBTN”: “3,5”, “TIME”: “70,23”,
“S1”: “24,44”, “S2”: “22,54”, “S3”: “23,25”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.05”, “3F 2F”: “11.49”, “2F 1F”: “11.63”, “1F 0”: “11.62”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “PRAWNS ELEVEN”, “GOING”: “G”,
“POS”: 11, “JOCKEY”: “H Bentley”, “TRAINER”: “M Newnham”, “DRAW”: 6, “TRACK”: “TURF B”,
“WT R”: 120, “DIST”: 1200, “HKR”: 65, “SP”: 10, “DBTN”: “4,75”, “TIME”: “70,44”,
“S1”: “24,60”, “S2”: “22,58”, “S3”: “23,26”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.13”, “3F 2F”: “11.45”, “2F 1F”: “11.59”, “1F 0”: “11.67”
},
{
“COURSE”: “HAPPY VALLEY”, “DATE”: “19/03/2025”, “HORSE”: “CALIFORNIA DEEPLY”, “GOING”: “G”,
“POS”: 12, “JOCKEY”: “M Chadwick”, “TRAINER”: “A S Cruz”, “DRAW”: 11, “TRACK”: “TURF B”,
“WT R”: 118, “DIST”: 1200, “HKR”: 63, “SP”: 65, “DBTN”: “5,25”, “TIME”: “70,50”,
“S1”: “24,68”, “S2”: “22,74”, “S3”: “23,08”, “S4”: “0”, “S5”: “0”, “S6”: “0”,
“4F 3F”: “11.25”, “3F 2F”: “11.49”, “2F 1F”: “11.55”, “1F 0”: “11.53”
}
]

Thanks
Br

Hello @Brain
I can see that your data string is a JSON structure. So it isn’t necessary the use of Py aiming to parse coded data to KNIME data frame. It would depend more on your data source and workflow connections

I’ve posted in HUB an example on how to proceed both: with the use of Py coding, or without -text variable-

If your source is a ‘.json’ file, you can use a ‘JSON Reader’ node as well (3rd option). Lets say, you can edit a ‘.txt’ file and mutate the extension to ‘.json’

Downstream the procedure is always mapping the columns to sets with the help of ‘JSON Path’ node, and ungroup them into a KNIME table data frame.

You can step into an ‘Excel Writer’ node afterwards, to achieve the data export…

BR

4 Likes

@gonhaddock It’s perfect as usual. You are one of my idols

Many thanks

2 Likes

PYTHON JSON HK Races.knwf (117.0 KB)
*
I try to use PYTHON JSON with another code and i get empty data …

Hello @Brain
There isn’t a ‘major’ problem on that. The differences are coming from Python itself. As you changed the input data shape, Py interprets the data on its own way based on the calling function requirements. In production data it is expected continuous unique format…

In the Community HUB’s Py example that I uploaded first, it interpreted a ‘tuple’ object; which generated a doble hierarchy level. However in your long commented data shape Py calls for a ‘list’, and returned only one hierarchical level.

One possible solution would be to remap the JSON Path node.

In the other hand I’ve upgraded the workflow in HUB, making it more robust; I forced the code to read the data as a list. And it’s already a remapped version of JSON Path to a single hierarchy.

You can download again the workflow, and use these nodes as template. Recommended to include the list() function, if your data is not uniform.

BR

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.