For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Searching a Json or XML structure for a specific key / value pair in Python
With data loaded from JSon or XML structures, you'll often want to search for attributes by element name - without having to code the structure, and with the code adapting itself to mean subtle structure changes in the suppied data.
Here's an example written during lat week's tailored Intermediate Python course
... taking a Json object (loaded from our web resource) and displaying a list of all the elements of a specific name.
As good python code encapsulate all the hard work into methods, our main progam is very short.
If I want to find the value of all the fields keyed "where" ... Loading the data
fh = open("jflow.json")
igot = json.load(fh)
found = parseDict(igot,"where")
and the hard work is done in parseDict as json.load returns a dict. parseDict has been written to return a list.
Sorting the results:
if sys.version_info < (3,0):
found.sort(key = lambda x:len(x))
and displaying the results, 4 per line.
print("Here are the remote IP visitors to your images")
for k in range(len(found)):
sep = ((k+1)%4 and k+1 != len(found)) and " " or "\n"
Full source code is [here]
. The data ('pretty printed' as Json compressed is hard to read!) is [here]
parseDict is a piece of recursive code - two re-entrant pieces of code which call each other as they traverse your structure and find lists and dicts within other lists and dicts. I am not
normally a fan of recursive / re-entrant code on courses, as they're typically over-clever solutions looking for problems, but this particular use is sensible.
I'll cover this for you on Python Intermediate courses
or on Python Programming courses
(in that case after class hours if you make a specific request, as not everyone wants to do this). (written 2016-10-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesY151 - Python & XML 
Handling XML in Perl - introduction and early examples - (2009-08-27) 
Good example of recursion in Python - analyse an RSS feed - (2009-11-18) 
Bookkeeping - (2009-12-29) 
XML handling in Python - SAX, DOM and XSLT examples - (2010-12-09) 
XML handling in Python - a new teaching example using etree - (2015-12-09)Y117 - Python - Already written modules 
Learning Python - many new example programs - (2009-01-31) 
Dates and times in Python - (2010-07-27) 
Syncronise - software, trains, and buses. Please! - (2010-08-22) 
A demonstration of how many Python facilities work together - (2011-09-16) 
How can I do an FTP transfer in Python? - (2011-10-05) 
Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14) 
JSON from Python - first principles, easy example - (2013-05-13) 
Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14) 
Reading command line parameters in Python - (2015-02-23) 
Binary data handling - Python and Perl - (2015-03-09) 
Programming with random numbers - yet re-using the same values for testing - (2016-06-22) 
Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23) 
Scons - a build system in Python - building hello world - (2016-10-29)
Some other Articles
The technical article feed continues - personal updates more proactive on Facebook now!Equality (in Python)A reminder of the key issues to consider in moving from Python 2 to Python 3Convering from Python 2 to Python 3 - an update, and the 2to3 utilitySearching a Json or XML structure for a specific key / value pair in PythonSome gems from Intermediate PythonSome gems from an introduction to PythonMelksham trial train service is to be made permanentCourse dates - from October 2016 to December 2017
4759 posts, page by page
Link to page ... 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, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).