This answer tells me that json starting with "[" is a list/array. Suffice to say, can several lists with dictionaries inside be considered valid json? As in [{"test": "test"}][{"test":"test"}] ?
I have some data returning that seems to be in this format but when I try and render the data inside a json viewer, it tells me it's the incorrect format. So maybe these viewers don't like json that's returned as a dictionary inside a list.
I'm not entirely sure if the data is in multiple lists but if I print "data[0] and then data1 then data[2] etc, it returns different dictionaries, so I assume it is a list. I would ideally like to loop through all the data so I could use a python for loop and say data[i] to go through all lists, but I suppose I would need to know what I am looping through.
From your example [{"test": "test"}][{"test":"test"}] you actually have two different lists. And each list has a dictionary within it.
JSON parsers expect to parse one entity, so you need to put those two lists within a larger list. Like this:
[ [{"test": "test"}], [{"test":"test"}] ]
Related
When trying to iterate through a defaultdict my variables were read as strings when they should be read as lists, however, when I changed my code a little bit, it worked but I don't know exactly why. My defaultdict is a dictonary that has a list of dictionaries inside it. The code looked like that
for engagement in engagement_by_account:
for engagement in engagement:
engagement['total_minutes_visited'] = float(engagement['total_minutes_visited'])
And the error was:
TypeError: string indices must be integers
However, when I changed the code to this:
for key,engagement in engagement_by_account.items():
for engagement in engagement:
engagement['total_minutes_visited'] = float(engagement['total_minutes_visited'])
there were no errors anymore.
By default, when you iterate over a dictionary (or defaultdict), you will iterate over the keys of that dictionary. It seems here that you wanted to iterate over the values so you could either do what you did or something like:
for engagements in engagement_by_account.values():
for engagement in engagements:
engagement['total_minutes_visited'] = float(engagement['total_minutes_visited'])
I have received a json file from an api and want to convert it from to a dictionary to a list in python. I already have it loaded as a python dictionary, so I'm really just looking to iterate over it and convert from a dictionary to a list. However, the dictionary has nested lists and not every object within the json file's many objects is always structured the same (i.e. the api will not always return the same json object for every event). I want to convert the dictionary to 3 separate lists, each list for a specific key:value pair I am parsing for.
I've tried using KeyError in a try/except statement to account for cases where there is no value but haven't had much luck. Also tried importing defaultdict from collections to no success as well. I gather I should be able to make the rests of my lists once I get some help with this first one. Hopefully I didn't overcomplicate the question
data = load_json()# function responsible for loading the json from a stored file
new_list = []
for some_key1 in data
if new_list['some_key1'] > 0:
try:
new_list.append(['some_key1'], some_key1)
except:
new_list.get(['some_key1'], '0')
for x in range(len(new_list)):
print(new_list[x])
I am looking to make one list for storing each object's (i.e. each python 'breaking' dictionary) key:value pair where some_key1 exists (i.e. this means in this case I will have a list for aDifferentCherry and yetAnotherDifferentCherry.
the python program that i am writing calls to an api that returns this json:
Code Output
How do i access the subdetails? When i run the .keys() it only lists those three top levels. I want to be able to get specific items, e.g. "Utility"
I've tried several solutions but none parse correctly. I have tried calling the list inside the dictionary, to no avail. Originally i thought it was a dictionary inside of a dictionary, but Python thinks its a list nested into a dictionary.
Any help would be appreciated!
keys() function only returns the keys of dictionary, so it you call keys(), it will only return the three result. The "subdetails" you are referring to are the values of those keys. For key "SUMMARY" as an example, its value is a list instead of dict (note the "[" after the key). However, the list only has a single element. This is quite common in json. To retrive "Utility", all you need to do is data['SUMMARY'][0]['Utility']
Maybe to help you understand the data structure better, call the "values()" and "items()" function to see what it returns.
Since it's a dict of lists of dicts, simply use an index of 0 to access the first item of the list if there is always only one item in each list. For example, if your JSON object is stored as variable data, then the value of Utility can be accessed with data['SUMMARY'][0]['Utility'].
Importing a JSON document into a pandas dataframe using records = pandas.read_json(path), where path was a pre-defined path to the JSON document, I discovered that the content of certain columns of the resulting dataframe "records" are not simply strings as expected. Instead, each "cell" in such a column is an array, containing one single element -- the string of interest. This makes selecting columns using boolean indexing difficult. For example, records[records['category']=='Python Books'] in Ipython outputs an empty dataframe; had the "cells" contained strings instead of arrays of strings, the output would have been nonempty, containing rows that correspond to python books.
I could modify the JSON document, so that "records" reads the strings in properly. But is there a way to modify "records" directly, to somehow strip the single-element arrays into the elements themselves?
Update: After clarification, I believe this might accomplish what you want while limiting it to a single iteration over the data:
nested_column_1 = records["column_name_1"]
nested_column_2 = records["column_name_2"]
clean_column_1 = []
clean_column_2 = []
for i in range(0, len(records.index):
clean_column_1.append(nested_column_1[i][0])
clean_column_2.append(nested_column_2[i][0])
Then you convert the clean_column lists to Series like you mentioned in your comment. Obviously, you make as many nested_column and clean_column lists as you need, and update them all in the loop.
You could generalize this pretty easily by keeping a record of "problem" columns and using that to create a data structure to manage the nested/clean lists, rather than declaring them explicitly as I did in my example. But I thought this might illustrate the approach more clearly.
Obviously, this assumes that all columns have the same number of elements, which maybe isn't a a valid assertion in your case.
Original Answer:
Sorry if I'm oversimplifying or misunderstanding the problem, but could you just do something like this?
simplified_list = [element[0] for element in my_array_of_arrays]
Or if you don't need the whole thing at once, just a generator instead:
simplifying_generator = (element[0] for element in my_array_of_arrays)
How do i load a text file full of 10 digit codes separated by a return into a dictionary in python?
Then how do i cross check the variables in the dictionary with my own variables?
Ok, it is simple really. I have a TXT file containing 1000 or so 10 digit sequences looks like this:
121001000
000000000
121212121
I need to input these files into a dictionary then be able to take a number that i receive and cross check it with this database so it does NOT match.
IE 0000000001 =/= any previous entry.
It sounds like you want to store the numbers in a way that makes it easy to look up "Is this other value already there?", but you don't actually have "values" to associate with these "keys" - so you don't really want a dict (associative array), but rather a set.
Python file objects are iterable, and iterating over them gives you each line of the file in turn. Meanwhile, Python's container types (including set) can be constructed from iterables. So making a set of the lines in the file is as simple as set(the_file_object). And since this is Python, checking if some other value is in the set is as simple as some_other_value in the_set.
On reading text from files, try looking over the python document for input/output. Additionally look through data structures tutorial.
Dictionary usually has a key and a value, that corresponds to the key:
name: "John"
age: 13
If you are just looking for the structure to read the values from the file, list seems to be more appropriate, since you did not specify anything about the designation of those values.
If you need the file's contents as numbers and not as strings:
file_data = set()
for line in open('/some/file/with/sequences.txt'):
file_data.add(int(line))
then later:
if some_num not in file_data:
do_something_with(some_num)
If you have blank lines or garbage in the file, you'll want to add some error checking.