Python how to POST already urlencoded form with requests module [closed] - python

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

Related

How can we use variable inside a quote in Python? [closed]

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 took a template file in terraform and I want to pass that variable in python, but throws an error each time I use.
{
'Name': 'tag:Name',
'Values': [value]
},
I want to pass the variable using this filter. Can someone help me with this?
you add the letter f before quotes and then use {} to display variables
i = 5
print(f"something {i}")
output: something 5

Python: how to extract the variables between 2 constant substring [closed]

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 am trying to extract the variables between 2 constant substring in a string. For example,
I wish to extract the variable Apple, Orange, Watermelon, Kiwi....13cups, 14cups...19cups. I am using the re expression to get to the first step of taking the variable between $ sign but I do not get anything results.
Anyone can advise on the correct expression or if there is a better way to extract it ?
Thanks.
import re
file = '$n$n$n$xa0$n$nSHOWALL$nSHOWALL%GROWTH$n$n$xa0$n$xa0$n$n$n$nApple$na$nOrange$n$nWatermelon$nKiwi$n$nBanana$nJackfruit$n$nGuava$na$nGrape$n$nPlum$na$nOrange$n$nCoconut$nWatermelon$n$n12cups$n13cups$n$n14cups$na$n15cups$n$n16cups$na$n17cups$n$n18cups$n19cups$n'
found = re.findall(r'(?=$(.*?)$)',file)
print(found)
Given that the rule(s) for identifying the required character sequences is ambiguous, I contend that RE is impractical. No doubt it could be done but here's a quick'n'dirty approach to the problem:-
data = '$n$n$n$xa0$n$nSHOWALL$nSHOWALL%GROWTH$n$n$xa0$n$xa0$n$n$n$nApple$na$nOrange$n$nWatermelon$nKiwi$n$nBanana$nJackfruit$n$nGuava$na$nGrape$n$nPlum$na$nOrange$n$nCoconut$nWatermelon$n$n12cups$n13cups$n$n14cups$na$n15cups$n$n16cups$na$n17cups$n$n18cups$n19cups$n'
for token in data.split('$n'):
if token not in ('SHOWALL%GROWTH', 'SHOWALL', '$xa0', 'a', ''):
print(token)

How do I get all first elements of python dictionary? [closed]

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 used dictionaries the first time and I can't figure out how to get all first elements of a dictionary. The picture shows an example of my problem. I want to get the brand names, not "brand0, brand1" etc.
thisdict = {
"brand0": ("Ford", "green_car"),
"brand1": ("Audi", "yellow_car"),
"brand2": ("Porsche", "red_car")
}
You can use several aproaches to this problem but the easiest is probably this
firstItems = [value[0] for value in thisdict.values()]
this works the same as
firstItems = []
for value in thisdict.values():
firstItems.append(value[0])

python string comparison failed [closed]

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
In python, I wrote a program to compare two strings.
The coedes are below
d = data.iloc[0]
cmpdate = d['quant_date']
print(cmpdate)
if (cmpdate=='2010-03-18'):
print("=================", dt)
else:
print("xxxxxxxxxxxxx", cmpdate)
the results are
2010-03-18
xxxxxxxxxxxxx 2010-03-18
tow strings are exactly same.
what is the problem?
TIA
Make sure you convert both of the dates to datetime format
and check the result
use to_datetime() function
This works fine

extract json by Python [closed]

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]

Categories

Resources