Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this JSON
myJson = [{"linkId":"changeDriveLink","url":"/drive/rayon.header.changedrivelink?t:ac=3686962/3696434","zoneId":"forceAjax"},
{"linkId":"affichePopinProduit","url":"/drive/rayon.header.promosmenu.thumbnailproduct.affichepopinproduit/804106/Paysan$0020Breton$0020beurre$0020moul$00e9$0020demi-sel$0020250g/$N?t:ac=3686962/3696434","zoneId":"forceAjax"},
{"linkId":"addProductToShoppingList2","url":"/drive/rayon.header.promosmenu.thumbnailproduct.addproducttoshoppinglist2/804106/Paysan$0020Breton$0020beurre$0020moul$00e9$0020demi-sel$0020250g?t:ac=3686962/3696434","zoneId":"forceAjax"}]
i want to have the URLS, i tried :
decoded = json.loads(myJson.read())
and
decoded = json.load(myJson.read())
and
decoded = json.load(myJson)
but I always have this error:
AttributeError: 'str' object has no attribute 'read'
How about:
decoded = json.loads(myJson)
which doesn't give any errors when I run it.
You don't need read(), just dump it (if you don't have it in string representation) and then load it.
import json
myJson = [{"linkId":"changeDriveLink","url":"/drive/rayon.header.changedrivelink?t:ac=3686962/3696434","zoneId":"forceAjax"},{"linkId":"affichePopinProduit","url":"/drive/rayon.header.promosmenu.thumbnailproduct.affichepopinproduit/804106/Paysan$0020Breton$0020beurre$0020moul$00e9$0020demi-sel$0020250g/$N?t:ac=3686962/3696434","zoneId":"forceAjax"},{"linkId":"addProductToShoppingList2","url":"/drive/rayon.header.promosmenu.thumbnailproduct.addproducttoshoppinglist2/804106/Paysan$0020Breton$0020beurre$0020moul$00e9$0020demi-sel$0020250g?t:ac=3686962/3696434","zoneId":"forceAjax"}]
myJson = json.dumps(myJson)
json.loads(myJson)
The result is a list of dicts:
[{'linkId': 'changeDriveLink',
'url': '/drive/rayon.header.changedrivelink?t:ac=3686962/3696434',
'zoneId': 'forceAjax'},
{'linkId': 'affichePopinProduit',
'url': '/drive/rayon.header.promosmenu.thumbnailproduct.affichepopinproduit/804106/Paysan$0020Breton$0020beurre$0020moul$00e9$0020demi-sel$0020250g/$N?t:ac=3686962/3696434',
'zoneId': 'forceAjax'},
{'linkId': 'addProductToShoppingList2',
'url': '/drive/rayon.header.promosmenu.thumbnailproduct.addproducttoshoppinglist2/804106/Paysan$0020Breton$0020beurre$0020moul$00e9$0020demi-sel$0020250g?t:ac=3686962/3696434',
'zoneId': 'forceAjax'}]
here is no need to 'loads' or 'dumps'. you are have already proper list-dict data.
for 'url's just use
[i['url'] for i in myJson]
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I am a beginner and need help with converting str to float on python.
the code seems to work fine locally, but when I deploy on heroku I get
"ValueError: could not convert string to float".
Is anybody else experiencing this? It would be great if you could help me.
2022-01-26T14:40:03.536858+00:00 app[web.1]:
tx_count = float(raw_data["result"].replace("0x", ""))
2022-01-26T14:40:03.536858+00:00 app[web.1]:
#ValueError: could not convert string to float: 'Invalid API Key'
my code:
my_address = request.form["public_key"]
response = requests.get(f"https://api.etherscan.io/api?module=proxy&action=eth_getTransactionCount&address={my_address}&tag=latest&apikey={API_KEY}")
raw_data = response.json()
raw_data:
raw_data = {
'jsonrpc': '2.0',
'id': 1,
'result': '0x3'
}
I want raw_data["result"] to be a number because I want to use the number to calculate.
I created a repository on github
https://github.com/yataishiba/co2-calc-eth
UPDATE
I tried this but it is still not working...
Convert hex string to int in Python
You can't throw hex directly at float() because the string can contain non-numerical strings; i.e. A-F.
Instead you should use float.fromhex(). This will also let you not use .replace(). For example:
tx_count = float.fromhex(raw_data["result"])
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have the following python code:
if str(fv["f"])=="AG":
nf=str(fv["f"])+" "+ in +" ("+"\'"+str(fv["v"])+"\'"+")"
which gives the output as:
nf=AG in ('abc,xyz,ef')
but the required output is:
nf=AG in ('abc','xyz','ef')
I tried placing split(",") at str(fv["v"]).split(",")
but it gives me TypeError: cannot concatenate 'str' and 'list' objects. please help how can I get the output.
str(tuple(str(fv["v"]).split(',')))
will probably give you what you want but if you give more information there is definitely a better way to write this.
What does fv['v'] consist of?
It depends on type of fv["v"] but probably this will work:
if str(fv["f"]) == "AG":
nf = "{} in {}".format(fv["f"], tuple(fv["v"]))
When you provide the result of type(fv["v"]) this answer would be corrected.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a problem that i cant post something that is already urlencoded
how it should be
dataa = {
client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer&client_assertion=eyJhbGci.....blahblahblah
}
r = scraper.post(url,headers=headers, data=dataa)
i have tried to make it like
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer&",
But it didnt work, any ideas guys?
You can use urllib.parse.unquote() to decode data:
from urllib.parse import unquote
source = "urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer&client_assertion=eyJhbGci"
result = unquote(source)
If you want to decode dict value dynamically, you can rewrite it:
dataa["client_assertion_type"] = unquote(dataa["client_assertion_type"])
Just discovered & in urldecoded string means new line, i used = as :
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
Hope that will help somebody
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
with open('twit/example.json', encoding='utf8') as json_data:
for line in json_data:
try:
dataText = json.loads(line)
except ValueError:
continue
for a in dataText:
print(a["user"]["location"])
the result is: string indices must be integers
Update: The below answer is for printing
print(dataText["user"]["location"])
now I want this one:
print(a["user"]["location"])
If your json file is in a normal format, use this instead:
with open('twit/example.json', encoding='utf8') as json_data:
dataText = json.loads(line)
for a in dataText:
print(dataText["user"]["location"])
The way your code is currently written makes me think you have multiple json structures in a single file, separated by new lines. This is not how json is usually formatted.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
with open('twit/example.json', encoding='utf8') as json_data:
for line in json_data:
try:
dataText = json.loads(line)
except ValueError:
continue
for a in dataText:
print(a["user"]["location"])
the result is: string indices must be integers
Update: The below answer is for printing
print(dataText["user"]["location"])
now I want this one:
print(a["user"]["location"])
If your json file is in a normal format, use this instead:
with open('twit/example.json', encoding='utf8') as json_data:
dataText = json.loads(line)
for a in dataText:
print(dataText["user"]["location"])
The way your code is currently written makes me think you have multiple json structures in a single file, separated by new lines. This is not how json is usually formatted.