Package: ezread
ezread provides a ridiculously simple way to fetch items within the JSON list.FeaturesRidiculously simple interface.Known JSON format for indexing helps understand what we are fetching and how.Easily procure relevant data from the JSON list for further processing.Natively supported nested indexing.SetupUsing pippipinstallezreadDirectly from the repositorygitclonehttps://github.com/csurfer/ezread.gitpythonezread/setup.pyinstallDocumentationModes of operationStrict: In this mode, the reader raises an exception if the “index” or the “key” to be read is not found.Non-Strict: In this mode, the reader simply returns None if the “index” or the “key” to be read is not found.API UsagefromezreadimportEzReader# For strict modereader=EzReader(<templatewithindex/keydetails>)reader.read(<jsontextwithlistofdictsorlists>)# For non strict modereader=EzReader(<templatewithindex/keydetails>,strict=False)reader.read(<jsontextwithlistofdictsorlists>)Template GuideThe template provided to EzReader is very important as it defines what “index” or “key” needs to be read. We expect the template to be in JSON format to provide it some structure. Let’s take a few examples and understand how you can specify the items to read from the JSON list that you have.Let’s take the following JSON list of lists.[[1,2,3,4,5,6,7,8,9,10],[2,4,6,8,10,12,14,16,18,20],[3,6,9,12,15,18,21,24,27,30]]Say you want to fetch 0th index of every list, what would the template look like?[0]The rows returned would be[1]
[2]
[3]Say you want to fetch 0th, 2nd and 4th index of every list, what would the template look like?[0,2,4]The rows returned would be[1, 3, 5]
[2, 6, 10]
[3, 9, 15]Let’s delve a little deeper into index templates with the following JSON list of dicts.[{"name":"Tom","age":30,"address":{"street":["124 Lincoln St","West Village"],"city":"New York","state":"NYC"}},{"name":"Dick","age":20,"address":{"street":["125 Lincoln St","West Village"],"city":"New York","state":"NYC"}},{"name":"Harry","age":40,"address":{"street":["50 Vinci Lane",""],"city":"San Fransisco","state":"CA"}}]Say you want to fetch name from every dict in the list, what would the template look like?["name"]The rows returned would be["Tom"]
["Dick"]
["Harry"]Say you want to fetch name and age from every dict in the list, what would the template look like?["name","age"]The rows returned would be["Tom", 30]
["Dick", 20]
["Harry", 40]Let’s say your query is a little bit complicated. You want to fetch name and city a person lives in, what would the template look like?You can use lists for nested indexing. Here you want to use “address” and from within it you want to fetch “city”. You can achieve it as follows["name",["address","city"]]The rows returned would be["Tom", "New York"]
["Dick", "New York"]
["Harry", "San Fransisco"]Does nested indexing always have to be dictionary keys?No nested indexing can be dictionary keys or (0-indexed) index within a list. Let’s fetch “name” and “first row of address” for each contact.["name",["address","street",0]]The rows returned would be["Tom", "124 Lincoln St"]
["Dick", "125 Lincoln St"]
["Harry", "50 Vinci Lane"]Non-Strict mode of querySo how does Non-Strict mode of query behave?Say we asked for “name” and “hometown” from the previous JSON example. Since “hometown” is not a key in the JSON, it would fail with “KeyError” in strict mode. Similarly if we were accessing a list and tried to access an index which is not present it would end up throwing “IndexOutOfBoundsError” in strict mode.The same query in non-srict mode would return the correct value for key/indexes it can fetch and None for others.["name","hometown"]The rows returned would be["Tom", None]
["Dick", None]
["Harry", None]Commandline toolCommandline tool provided with this library serves as an easy way to fetch the needed items as CSV file output.Usage# For helpezread--help# To use template string directlyezread--template_str<indextemplatestring><jsonfiletoread># To use template string from a fileezread--template_file<filewithindextemplatestring><jsonfiletoread># By default it uses "," as the separator. If you want a different separator you can use --separator option.
# We use the strict mode by default. If you want to use non-strict mode use --nonstrictContributingBug Reports and Feature RequestsPlease useissue trackerfor reporting bugs or feature requests.DevelopmentPull requests are most welcome.Buy the developer a cup of coffee!If you found the utility helpful you can buy me a cup of coffee using