internal error occured after json decoding 1100 elements using python - python

I am using python to decode an api response,which is in json format.After 1100 data checking this error occurs
<Response [200]>
{'error': 'internal error'}
Traceback (most recent call last):
File "E:/freelancer_projects/ahref/app2.py", line 63, in <module>
get_rating()
File "E:/freelancer_projects/ahref/app2.py", line 46, in get_rating
result.append((u,data["domain"]["domain_rating"]))
KeyError: 'domain'
As you can see the response from the api is ok,but the error just saying its internal.Why is this error and what will be its fix ? I am using requests library to get data from api
full code:
def get_rating():
fr = input("Enter filename of urls:")
token = input("Enter token:")
f = open(fr, "r",encoding="utf8")
urls = f.readlines()
result=[]
count=1
for u in urls:
u = u.replace("http://", "")
u = u.replace("https://", "")
if "www" not in u:
u="www"+u
ind = u.find("www")
u = u[ind:]
u=u.replace('\n', '')
u=u[:-1]
# print(u)
data=getPageData(u,token)
if data != "":
print(data)
# print(data)
# print(data["domain"]["domain_rating"])
result.append((u,data["domain"]["domain_rating"]))
print(count)
count+=1
# print(result)
result=sorted(result,key=lambda x:x[1],reverse=True)
# print(result)
saveData(result)
def saveData(result):
wb = openpyxl.Workbook()
sheet =wb.get_sheet_by_name('Sheet')
sheet.cell(row=1, column=1).value = "URL"
sheet.cell(row=1, column=2).value = "DOMAIN RATING"
for index,r in enumerate(result):
sheet.cell(row=index+2, column=1).value = r[0]
sheet.cell(row=index+2, column=2).value = r[1]
wb.save("output.xlsx")
get_rating()

In this line of your code:
result.append((u,data["domain"]["domain_rating"]))
You are trying to access a key in the data dictionary that does not exist, resulting in your error:
KeyError: 'domain'
You'd best check if the key exists in your dictionary before accessing, like this:
if "domain" in data:
print('found domain')
On a side note, using this code
if data != ""
to check your dictionary isn't a very good idea, it will always be True, see this example in Python interpreter:
>>> mydict = dict(foo='foo', bar='bar')
>>> mydict
{'foo': 'foo', 'bar': 'bar'}
>>> mydict != ""
True
>>> mydict2 = dict()
>>> mydict2
{}
>>> mydict2 != ""
True
>>> mydict2 = None
>>> mydict2 != ""
True
As you can see, with an empty dict, and even if we set mydict2 to None, we satisfy the condition, but your code below would fail.

Related

Import file into a json key

I want my program to go into a file and take all of the lines and put them into a json key.
The code:
def import_proxies():
global proxies_json
proxies_json = {'http': {}}
with open("proxies.txt", 'r', encoding='utf8') as proxy_file:
for line in proxy_file:
val = line.split()
proxies_json[['http'][val]] = val
print(proxies_json)
import_proxies()
My erorr: TypeError: list indices must be integers or slices, not list
What I want it to do is is import the file lines as values in the http key (proxies_json = {'http': {}}
proxies_json seems to be a dictionary. So something like this should work
def import_proxies():
global proxies_json
proxies_json = {'http': {}}
with open("proxies.txt", 'r', encoding='utf8') as proxy_file:
for line in proxy_file:
val = line.split()
data = proxies_json.get('http', {})
for item in val:
data[item] = item
proxies_json['http'] = data
print(proxies_json)

Syntax Error invalid syntax for def Data(path)

Here is the line of code where I am getting an error
def Data('C:/Users/username/Desktop/d.txt'):
fp = open('C:/Users/username/Desktop/d.txt')
I get the following error:
File "read.py", line 17
def DataExtract('C:/Users/username/Desktop/d.txt'):
^
SyntaxError: invalid syntax
Not sure what the problem is and what is causing the invalid syntax
Thank you!
Here is the code I am working on:
def DataExtract('C:/Users/username/Desktop/d.txt'):
fp = open('C:/Users/username/Desktop/d.txt')
data = {}
line = fp.read().split("\n")[2:7:5]:
while line:
name, _, cont = line.partition(":")
keys, _, values = cont.partition("=")
keys = keys.split(",")
values = values.split(",")
temp_d = {}
for i in range(len(keys)):
key = key[i].strip()
val = values[i].strip()
temp_d[key] = float(val)
data[name] = temp_d
line = fp.readline()
fp.close()
return data
x = DataExtract('C:/Users/username/Desktop/d.txt')
mul_p = x['d1']['p'] * x['d2']['p']
print(mul_p)
As mentioned earlier in the comments by #LhasaDad.
I am observing two errors in your attached code.
First Error:
First is the "def" statement. The function needs a variable placeholder not the value of the variable. For example:
>>> def fun('hello'):
File "<stdin>", line 1
def fun('hello'):
^
SyntaxError: invalid syntax
>>> def fun(5):
File "<stdin>", line 1
def fun(5):
^
SyntaxError: invalid syntax
The correct way is
>>> def fun(var):
... print(var)
...
>>> #Then call the function using below method
...
>>> fun('hello')
hello
>>> fun(5)
5
Second Error:
There should always be and indentation (tab or 4 spaces gap after a def statement)
So in your case hopefully this will work
def DataExtract(file_path):
fp = open(file_path)
data = {}
line = fp.read().split("\n")[2:7:5]
while line:
name, _, cont = line.partition(":")
keys, _, values = cont.partition("=")
keys = keys.split(",")
values = values.split(",")
temp_d = {}
for i in range(len(keys)):
key = keys[i].strip()
val = values[i].strip()
temp_d[key] = float(val)
data[name] = temp_d
line = fp.readline()
fp.close()
# Now call the function as
DataExtract('C:/Users/username/Desktop/d.txt')
You can also read how to use python functions from def
https://www.w3schools.com/python/python_functions.asp

Splitting a file into a dictionary in Python

I'm only a beginner python user so my apologies if this is a rather simple question. I have a file containing two lists divided by a tab. I would like to store this in a dictionary, so each entry is associated with the corresponding entry after the tab, such that:
cat hat
mouse bowl
rat nose
monkey uniform
dog whiskers
elephant dance
would be divided into
{'cat'; 'hat', 'mouse' ; 'bowl') etc. etc.
It's a very long list.
This is what I tried:
enhancerTAD = open('TAD_to_enhancer.map', 'r')
list = enhancerTAD.split()
for entry in list:
key, val = entry.split('\t')
ET[key] = val
print ET
Here's my most recent attempt, and the error message that I get below:
enhancerTAD = open('TAD_to_enhancer.map', 'r').read()
ET = {}
lst = enhancerTAD.split("\n")
for entry in lst:
key, val = entry.strip().split(' ',1)
ET[key] = val
enhancergene = open('enhancer_to_gene_map.txt', 'r').read()
GE = {}
lst1 = enhancergene.split("\n")
for entry in lst1:
key, val = entry.strip().split(' ',1)
GE[key] = val
geneTAD = open('TAD_to_gene_map.txt', 'r').read()
GT = {}
lst2 = geneTAD.split("\n")
for entry in lst2:
key, val = entry.strip().split(' ',1)
GT[key] = val
File "enhancertadmaybe.py", line 13, in
key, val = entry.strip().split(' ',1)
ValueError: need more than 1 value to unpack
You can try:
with open('foo.txt', 'r') as f:
print dict(line.strip().split('\t', 1) for line in f)
Result:
{'monkey': 'uniform', 'dog': 'whiskers', 'cat': 'hat', 'rat': 'nose', 'elephant': 'dance', 'mouse': 'bowl'}
Modification to your original method :
enhancerTAD = open('TAD_to_enhancer.map', 'r').read()
ET={}
lst = enhancerTAD.split("\n")
for entry in lst:
key, val = entry.strip().split('\t',1)
ET[key] = val
print ET
Points:
1.Your original method failed because were trying to split on a file object not the file content
i.e)
a=open("amazon1.txt","r")
c=a.split()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'file' object has no attribute 'split'
2.You have to read the content of the file to split it
i.e.)
enhancerTAD =open("amazon1.txt","r").read()
3.Since you have key,value pair in each line you have to split at new line initially
4.Then you can iterate over the list and again split it at \t and form the dictionary
Juniorcomposer method does all of this two line code and is more pythonic

Parse the file section wise in python

I have a text (abc.txt) file having the following entry in text file:
[General]
Local=C:\Work\July\
path=C:\Work\July\abc
[Field1]
BB0B2BA8--EFE4-4567-B8AE-0204D4BF9F60=
[CustDetails]
BB0B2BA8-EFE4-4567-B8AE-0204D4BF9F60=NOthing
[DirName]
8e27822e-5f46-4f41=TEST
[URLNAME]
8e27822e-5f46=https://
[DestURL]
8e27822e-5f46=some_URL
I want to parse the abc.txt file and take into variable. like in variable
MYpath = C:\Work\July\abc
custdetails= Nothing
dir_name = TEST
URL_Name = https://
DestURL = some_URL
Thanks,
Using ConfigParser:
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('abc.txt')
dic = {}
for section in config.sections():
for option in config.options(section):
res = config.get(section, option)
if res == '':
continue
dic.update({section: res})
print dic
Output:
{'DestURL': 'some_URL', 'URLNAME': 'https://', 'CustDetails': 'NOthing', 'DirName': 'TEST', 'General': 'C:\\Work\\July\\abc'}
You can use a dict here:
>>> dic = {}
with open('abc.txt') as f:
data = f.read()
lines = data.split('\n\n')
for line in lines:
line = line.split('\n')
field = line[0].strip('[]')
val = line[-1].split('=')[1]
if val:
dic[field] = val
...
>>> dic
{'DestURL': 'some_URL',
'URLNAME': 'https://',
'CustDetails': 'NOthing',
'DirName': 'TEST',
'General': 'C:\\Work\\July\\abc'}

'dict' object has no attribute 'loads'

Im trying to create a simple key/value store client that accepts input from a text file like this
list
set foo bar
get foo
list
This is the error I am getting
Traceback (most recent call last):
File "hiringAPIv1.py", line 37, in <module>
json = json.loads(handle.read())
AttributeError: 'dict' object has no attribute 'loads'
I currently have the whole thing in a for loop to call the url for each new command but I'm having issues with any input file larger than 1 line. Here is my source, i'm using Python.
import urllib
import urllib2
import fileinput
import json
import pprint
urlBase = "http://hiringapi.dev.voxel.net/v1/"
f = file('input.txt', 'r')
for line in f:
contents = line.split()
#print contents
#print len(contents)
if len(contents) >= 1:
command = contents[0]
if len(contents) >= 2:
key = contents[1]
if len(contents) >= 3:
value = contents[2]
if command == 'get':
urlFinal = urlBase + "key?key=" + key
output = key
if command == 'set':
urlfinal = urlBase + "key?key=" + key + "&value=" + value
output = 'status'
if command =='list':
urlFinal = urlBase + command
#if command == 'delete':
response = urllib2.Request(urlFinal)
try:
handle = urllib2.urlopen(response)
json = json.loads(handle.read())
#pprint.pprint(json)
#pprint.pprint(json['status'])
if command == 'list':
print str(json['keys'])
else:
print str(json[output])
except IOError, e:
if hasattr(e, 'code'):
print e.code
if hasattr(e,'msg'):
print e.msg
if e.code != 401:
print 'We got another error'
print e.code
else:
print e.headers
print e.headers['www-authenticate']
f.close()
You replaced your json module with the results of the .loads() function:
json = json.loads(handle.read())
Don't do that. Use a different name to store your data. Use data perhaps:
data = json.loads(handle.read())
if command == 'list':
print(data['keys'])
else:
print(data[output])
Note that the json module also has a .load() function (no s at the end) that takes a file-like object. Your handle is such an object, so you can use json.load() instead and have that read the response:
data = json.load(handle)
if command == 'list':
print(data['keys'])
else:
print(data[output])
Note that I pass in just the handle, we don't call .read() anymore. I also removed the str() calls; print handles that for us.

Categories

Resources