Can I edit dictionaries created by dictreader in python - python

I am trying to edit values in dictionaries created by dictreader. If I understand this correctly, it creates a list of dictionaries. This part is working fine. However, I'd like to change some of the values for the key:value pairs in certain dictionaries and this is not functioning the way I expect. After I finish editing the values, my dictreader object seems to be completely empty.
To deal with this, I make an empty list (elis) and then fill it with all the dictionaries that were in the dictreader object.
My code follows:
import csv
import numpy as np
import pandas as pd
# Load the csv into a dictreader object
elistable = csv.DictReader(open("./elispotable.csv", 'rU'))
# This code will print all the dictionaries when uncommented
# for item in elis:
# print item
# Create an empty list
elis=[]
#Note that the dicts have keys including 'Control' and 'Peptide Id'
for item in elistable:
if item['Control']=='Neg':
item['Peptide Id']='Neg'
if item['Control']=='Pos':
item['Peptide Id']='Pos'
elis.append(item)
#Elis is now full with all the dictionaries and correct values that were edited in the for loop
dude=[]
for item in elistable:
dude.append(item)
print dude
Dude is totally empty and if I try to run a for loop to print the dicts from elistable, nothing prints. It works before I run the for loop with if statements, but not after. It seems to be completely empty now.
Is it possible to edit the values in the dictionaries created by the dictreader? Am I somehow overwriting them all?

If I understand this correctly, it creates a list of dictionaries.
It doesn't; a DictReader is an iterator that generates the next dictionary when you request it. As with all Python iterators (and as with regular file object iteration), if you try to iterate over it twice, the second loop will see the iterator as empty, since the iterator doesn't go back to the start after the first loop.
If you want a list, call list on it:
elistable = list(csv.DictReader(open("./elispotable.csv", 'rU')))
Separate loops will then use separate iterators over the list.

Related

Looping Through Dictionaries Inside a List

I have a JSON file I'm trying to iterate through. The code to print the part I want is: print(data['data'][0]['text']) But I also want to print print(data['data'][1]['text']) and print(data['data'][2]['text']) and print(data['data'][3]['text']) and so forth and so on. I just can't figure out how to loop it since the JSON object is so convoluted.
You need to iterate over the list that is data['data'].
for item in data['data']:
print(item['text'])

Add selenium data to excel PYTHON

I have a problem. I have a code that using selenium and getting information form different sites and put then into one list. And after all, python will delete all information in the list, I need to write them to the excel:
List = []
for values in List:
...
List.append(values)
List.append(some_information_from_selenium)
And in the end of iteration:
List.clear()
I need to save information before clear() and after cleat List, add new information to the excel. This iteration have limit - 100. Need to create a new excel file, and adding information to this. List will delete and then append new information, this iteration will be 100 times. I will have 18 columns and 100 rows. I can use whatever i want.
:UPD:
One more question: if i use
data = pd.DataFrame()
data({ "Name":List[some_index]
"Surname":List[some_index_1]
.... })
data.to_excel("Excel.xlsx")
Why I got error 'DataFrame' object is not callable and how can i solve this
I'm not 100% sure what you're trying to do from your code, but instead of clearing your list variable each time lets hold it in some sort of nested collection.
a simple dictionary will do.
from collections import defaultdict
data_dict = defaultdict(list)
for num in range(10,110,10): #call your func in iterations of 10s
your code
data_dict[i].append(some_information_from_selenium)
each iteration of 10 will hold your nested data.
data_dict[10]
which you can then pass into pandas.

Conv json obj w/ varying keys & key:value pairs to python list for specific keys:values

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.

How to add json dictionaries to a list of lists

I have created some code to merge json documents (aka dictionaries) that I need to merge by looping through them and then adding them to a list of 'merged' json documents (myList).
So myList is a list of lists containing jsons!
Here is a snapshot of the code that is adding the 2 jsons. It took me a while to figure out a solution and only after I added the first line (it appears I have to initiate a new list), it worked. Otherwise I would get an 'out of index-range' error.
Is there a simpler way of doing this?
for ....
myList.append([])
myList[cnt].append(dict1)
myList[cnt].append(dict2)
cnt += 1
As mer mezba's comment. A much simpler way of doing this is
for ....
myList.append([ dict1, dict2 ])
cnt += 1
This will create a new list within mylist and add both json docs (as dictionaries). Adding to an existing list can still be done using e.g.
myList[someExistingIndex].append(dict1)

How do i access the sub details in the JSON using python?

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'].

Categories

Resources