'Using a list element to retrieve a value from e result object
I'm new to Python. I'm trying to get a subset of whois info for a list of IP addresses.
These statements worked:
01 oWhois = IPWhois(sIP)
02 resWhoisInfo = oWhois.lookup_whois()
03 sWhois_CIDR = resWhoisInfo["nets"][0]['cidr']
04 sWhois_Name = resWhoisInfo["nets"][0]['name']
0N ...
Until I got a result that was incomplete and I got an index error.
So rather than putting an if/else statement around lines 03, 04, 0N, I thought I'd be clever and define the following:
nWhoisCIDR = 0
nWhoisName = 1
nWhoisCountry = 2
nWhois... = N
lWhoisItems=[[nWhoisCIDR, 'cidr'],[nWhoisName, 'name'],[nWhoisCountry, 'country'],[nWhois..., 'whatever']]
So that I could use a loop and reference items like this:
for i in lWhoisItems:
try:
lWhoisResults = lWhoisResults + resWhoisInfo["nets"][0][(lWhoisItems[i][1])]
except IndexError:
lWhoisResults = lWhoisResults + "no value found"
But when I try to that I get an error: "TypeError: list indices must be integers or slices, not list" - even though (lWhoisItems[0][1]) == 'cidr'... (and so on).
So my question, how can I make this work? Or is this style of programming just not very Pythonish? If not could you please point me to (an example of) a solution that pretty much does the same?
Thank you very much!!!
Solution 1:[1]
You were almost right you just missed this detail. If you print i in the for loop by adding this line here.
for i in lWhoisItems:
print(i)
try:
lWhoisResults = lWhoisResults + resWhoisInfo["nets"][0][(lWhoisItems[i][1])]
except IndexError:
lWhoisResults = lWhoisResults + "no value found"
You will get the output something like this
[0, 'cidr']
[1, 'name']
[2, 'country']
...
So you can use unpacking to get the index you need.
for i, key in lWhoisItems:
try:
lWhoisResults = lWhoisResults + resWhoisInfo["nets"][0][lWhoisItems[i]]
except IndexError:
lWhoisResults = lWhoisResults + "no value found"
You can do even better by accessing the key itself.
for i, key in lWhoisItems:
try:
lWhoisResults = lWhoisResults + resWhoisInfo["nets"][0][key]
except IndexError:
lWhoisResults = lWhoisResults + "no value found"
Solution 2:[2]
Couldn't wait so tried right away :) The "+" generates errors, so this is the way to go:
for i, key in lWhoisItems:
try:
lWhoisResults.append(quotestr(resWhoisInfo["nets"][0][key]))
except IndexError:
lWhoisResults.append("no value found")
Also, since I'm writing this to a csv file and lWhoisResults is obviously a list, one should do the following:
fOutput.write("\n" + sIP + "," + sHost + "," + "," .join(lWhoisResults))
Don't know why the 2nd comma is needed, but that produces the correct results. Otherwise the first value in the list is concatenated to the value of "sHost".
Thanks again, this is getting me what I need.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yannis P. |
| Solution 2 | TheLonePilot |
