Add selenium data to excel PYTHON - 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.

Related

loop over a list of json dictionary objects to return values

This command in python returns the below image:
incidents = res[0].get("Contents", [{}])
return_results(incidents)
I want to iterate over the data objects and pull back values.
Such as:
for incident in incidents:
lowestLevel = incident.get("Contents", {}).get("data")
return_results(lowestLevel.get('id'))
I can't figure how to loop over the data to get the id for each "data set"
Anyone have any thoughts, let me know what I can expand on,
Thanks,
Boyd
In the screenshot, the incidents have 2 values.
I do think the data you needed is inside the root.data.
you need something like for looping for item in lowestlevel['root']['data']:
If incidents = res[0].get("Contents", [{}]) and for incident in incidents: are both talking about the same incidents then it looks as though you are iterating over the dictionary (JSON) Objects in an array.
For your implementation would the following suffice?:
for incident in incidents:
for lowestLevel in incident.values():
for data in lowestLevel.get("data")
return_results(data.get("id"))
Also here are suggested looping techniques available in python's documentation.

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)

Can I edit dictionaries created by dictreader in 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.

Python Programming approach - data manipulation in excel

I'm using the python packages xlrd and xlwt to read and write from excel spreadsheets using python. I can't figure out how to write the code to solve my problem though.
So my data consists of a column of state abbreviations and a column of numbers, 1 through 7. There are about 200-300 entries per state, and i want to figure out how many ones, twos, threes, and so on exist for each state. I'm struggling with what method I'd use to figure this out.
normally i would post the code i already have but i don't even know where to begin.
Prepare a dictionary to store the results.
Get the numbers of line with data you have using xlrd, then iterate over each of them.
For each state code, if it's not in the dict, you create it also as a dict.
Then you check if the entry you read on the second column exists within the state key on your results dict.
4.1 If it does not, you'll create it also as a dict, and add the number found on the second column as a key to this dict, with a value of one.
4.2 If it does, just increment the value for that key (+1).
Once it has finished looping, your result dict will have the count for each individual entry on each individual state.
I'm going to assume you already know how to do to the easy part of this and read a spreadsheet into Python as a list of lists. So, you've got something like this:
data = [['CA', 1],
['AZ', 2],
['NM', 3],
['CA', 2]]
Now, what you want for each state, for each number, a count of the number of times that number appears. So:
counts = {}
for state, number in data:
counts.setdefault(state, collections.Counter())[number] += 1

Categories

Resources