I'm calling an API that will require dynamic variables as parameters. However, I do not know how to format the string to include variables when it is surrounded by triple quotes and a backslash escape character at the beginning of the string literal.
I've tried varying the amount of quotes and using the ".format()" function.
Here is code formatted in a way that gets a successful result:
payload = "{\n\t\"firm\": \"myfirm\",\n\t\"id\": \"f87987562\",\n\t\"data\": {\n\t\t\"tracking_preference\": 2\n\t} \n}\n"
Here is my attempt at trying to format the string in a cleaner way while also including variables:
payload = \
"""{
"firm": {0},
"id": {1},
"data": {
"tracking_preference": {2}
}
}
""".format('myfirm', "f87987562", 2)
This is the error that I am receiving:
19 }
20 }
---> 21 """.format('myfirm', "f87987562", 2)
22
23 apikey = "secret_key"
KeyError: '\n "firm"'
I suspect it has something to do with the backslash, but its implementation seems necessary. Any help and insight on the intuition behind this string formatting is greatly appreciated.
I am trying to pass the string literal into the request function:
response = requests.request("POST", url, data=payload, headers=headers)
In a format string, { and } are special. To embed a literal parenthesis, use {{ or }}.
payload = """{{
"firm": "{0}",
"id": "{1}",
"data": {{
"tracking_preference": {2}
}}
}}
""".format('myfirm', "f87987562", 2)
print(payload)
Output:
{
"firm": "myfirm",
"id": "f87987562",
"data": {
"tracking_preference": 2
}
}
In Python 3.6+, f-strings can make this simpler:
firm = 'myfirm'
id = 'f87987562'
tracking = 2
payload = f'''{{
"firm": "{firm}",
"id": "{id}",
"data": {{
"tracking_preference": {tracking}
}}
}}'''
Finally, the json module is ideal for this specific scenario:
import json
firm = 'myfirm'
id = 'f87987562'
tracking = 2
data = {'firm':firm,'id':id,'data':{'tracking_preference':tracking}}
payload = json.dumps(data,indent=2) # indent is optional for easy reading
print(payload)
Output:
{
"firm": "myfirm",
"id": "f87987562",
"data": {
"tracking_preference": 2
}
}
If you're using python 3.6+, you could use an f-string:
payload = \
f"""{
"firm": {"myfirm"},
"id": {"f87987562"},
"data": {
"tracking_preference": {2}
}
}
"""
If not, you might be better off with a string template:
from string import Template
payload_t = Template(
"""{
"firm": ${firm},
"id": ${id},
"data": {
"tracking_preference": ${tracking}
}
}
""")
payload = payload_t.substitute(firm="myfirm", id="f87987562", tracking=2)
Related
I have a MongoDB NoSQL database, the name is baike, there is a collection named baike_items with the following format:
id:
title:
baike_id
page_url
text
All other fields are fine except the page_url. Some of the urls are normal like:
'https://baike.baidu.hk/item/%E5%A5%91%E4%B8%B9%E6%97%8F/2390374'
But some urls are ended with a string #viewPageContent, like:
https://baike.baidu.hk/item/%E5%E6%97%8F/11435374#viewPageContent
My intention is to write a mongoDB query to remove all the urls' #viewPageContent string while keep the rest of the string.
https://baike.baidu.hk/item/123#viewPageContent
https://baike.baidu.hk/item/456#viewPageContent
.
.
.
to
https://baike.baidu.hk/item/123
https://baike.baidu.hk/item/456
.
.
.
Any suggestions? thanks.
update1
The following python should do it.
db.baike_items.update_many(
{ "page_url": { "$regex": "#viewPageContent"} },
[{
"$set": { "page_url": {
"$replaceOne": { "input": "$page_url", "find": "#viewPageContent", "replacement": "" }
}}
}]
)
old_url = "https://baike.baidu.hk/item/%E7%89%A9%E7%90%86%E5%85%89%E5%AD%B8/61334055#viewPageContent"
new_url = old_url.replace("#viewPageContent", "")
print(old_url)
>>> https://baike.baidu.hk/item/%E7%89%A9%E7%90%86%E5%85%89%E5%AD%B8/61334055#viewPageContent
print(new_url)
>>> https://baike.baidu.hk/item/%E7%89%A9%E7%90%86%E5%85%89%E5%AD%B8/61334055
import re
a = "https://baike.baidu.hk/item/%E7%89%A9%E7%90%86%E5%85%89%E5%AD%B8/61334055#viewPageContent"
print(re.sub(r"#viewPageContent", '', a))
output: https://baike.baidu.hk/item/%E7%89%A9%E7%90%86%E5%85%89%E5%AD%B8/61334055
Hope I could help you!
db.baike_items.update_many(
{ "page_url": { "$regex": "#viewPageContent"} },
[{
"$set": { "page_url": {
"$replaceOne": { "input": "$page_url", "find": "#viewPageContent", "replacement": "" }
}}
}]
)
I have testdata read from excel and want it used to format a nested json string for request post body used, details as below:
import json
kwargs = {"name": "testname", "device": {"plt": "dsk"}}
payload = """
{{
"name": "{name}",
"device": "{device}",
}}
"""
payload = json.loads(payload.format(**kwargs))
And the expected payload should be:
{
"name": "testname",
"device": {"plt": "dsk"}
}
But there's error with json.loads(payload.format(**kwargs)), then how to format nested json string with **dict?
error message
You should do the operation in steps to figure out what goes wrong.
kwargs = {"name": "testname", "device": {"plt": "dsk"}}
payload = """
{{
"name": "{name}",
"device": "{device}",
}}
"""
json_string = payload.format(**kwargs)
print(json_string)
gives
{
"name": "testname",
"device": "{'plt': 'dsk'}",
}
Notice the dictionary {'plt': 'dsk'} was stringified python-style before being interpolated into the payload string. This is not valid json -- the apostrophes need to be replaced with quotation marks.
To get around this, I suggest you create a python dict that looks like payload and then convert that to a json string.
# Some preexisting dictionary
payload_dict = {"oldvalue": 0}
# Add values from kwargs
for key, value in kwargs.items():
payload_dict[key] = value
# Dump it to json
payload = json.dumps(payload_dict, indent=4) # indent=4 is only for pretty-print
print(payload)
which gives the output:
{
"oldvalue": 0,
"name": "testname",
"device": {
"plt": "dsk"
}
}
If payload_dict is identical to kwargs, then all you need to do is
payload = json.dumps(kwargs)
print(payload)
{
"name": "testname",
"device": {
"plt": "dsk"
}
}
I have a dictionary below:
event = {
"body-json": {},
"params": {
"path": {
"matchphrase": "term"
},
"querystring": {
"dataproduct.keyword": "health"
},
"header": {
"Accept": "application/json"
}
},
"resource-path": "/{matchphrase}"
}
I would like to access the above event dictionary keys & values and frame a new dictionary as follows:
{"query": {"term" : {"dataproduct.keyword": "health"}}}
Here is the code what I tried:
a = event['params']['path']['matchphrase'] #term
b = list(event['params']['querystring'].keys())[0] #dataproduct.keyword
c = list(event['params']['querystring'].values())[0] #health
body=f"{query: {{a} : {{b}: {c}}}}"
print(body)
Am I missing something ?
This should work :
body = {"query":{str(a):{str(b):str(c)}}}
print(body)
The escaping is wrong.
Try this instead:
body = f'{{"query": {{{a!r}: {{{b!r}: {c!r}}}}}}}'
I've also added !r which will return the real representation (repr) of the object (so you don't need to artificially add quotes).
you can create a dictionary and then get a string version of it using json.dumps.
import json
event = {
"body-json": {},
"params": {
"path": {"matchphrase": "term"},
"querystring": {"dataproduct.keyword": "health"},
"header": {"Accept": "application/json"},
},
"resource-path": {"matchphrase}"},
}
a = event["params"]["path"]["matchphrase"] # term
b = list(event["params"]["querystring"].keys())[0] # dataproduct.keyword
c = list(event["params"]["querystring"].values())[0] # health
result = {"query": {a: {b: c}}}
print(json.dumps(result))
Output:
{"query": {"term": {"dataproduct.keyword": "health"}}}
How can I make a string from json text when the json text contains many, many quotation marks and string escapes?
For example, the following works:
json_string = """
{
"styles":[
{
"label":"Style",
"target":{
"label":"Target"
},
"overrides":{
"materialProperties":{
"CRYPTO_ID":{
"script":{
"binding":"name"
}
}
}
}
}
]
}
"""
However this does not, due to the escapes:
new_string = """
{
"styles":[
{
"label":"Style",
"target":{
"label":"Target",
"objectName":"*"
},
"overrides":{
"materialProperties":{
"perObj":{
"script":{
"code":"cvex myFn(string myObj=\"\"; export string perObj=\"\") { perObj = myObj; } ",
"bindings":{
"myObj":"myObj"
}
}
}
}
}
}
]
}
"""
Is there a smart way to break this up? I've had no luck breaking it out into chunks and re-assembling to form the same thing when joined and printed.
Your string per se is valid JSON, however Python still sees the \ as special characters.
Use a raw string by prefixing your string with r:
import json
new_string = r"""
{
"styles":[
{
"label":"Style",
"target":{
"label":"Target",
"objectName":"*"
},
"overrides":{
"materialProperties":{
"perObj":{
"script":{
"code":"cvex myFn(string myObj=\"\"; export string perObj=\"\") { perObj = myObj; } ",
"bindings":{
"myObj":"myObj"
}
}
}
}
}
}
]
}
"""
json.loads( new_string )
Or escape your \ characters:
import json
new_string = """
{
"styles":[
{
"label":"Style",
"target":{
"label":"Target",
"objectName":"*"
},
"overrides":{
"materialProperties":{
"perObj":{
"script":{
"code":"cvex myFn(string myObj=\\"\\"; export string perObj=\\"\\") { perObj = myObj; } ",
"bindings":{
"myObj":"myObj"
}
}
}
}
}
}
]
}
"""
json.loads( new_string )
I would recommend reading from an actual JSON file rather than embedding it into your Python code:
with open('path/to/file.json') as f:
json_string = f.read()
Or, if you need the JSON parsed into Python objects (dicts, lists etc.):
import json
with open('path/to/file.json') as f:
json_data = json.load(f)
I'm reading in a JSON string which is littered with u'string' style strings. Example:
[
{
"!\/award\/award_honor\/honored_for": {
"award": {
"id": "\/en\/spiel_des_jahres"
},
"year": {
"value": "1996"
}
},
"guid": "#9202a8c04000641f80000000003a0ee6",
"type": "\/games\/game",
"id": "\/en\/el_grande",
"name": "El Grande"
},
{
"!\/award\/award_honor\/honored_for": {
"award": {
"id": "\/en\/spiel_des_jahres"
},
"year": {
"value": "1995"
}
},
"guid": "#9202a8c04000641f80000000000495ec",
"type": "\/games\/game",
"id": "\/en\/settlers_of_catan",
"name": "Settlers of Catan"
}
]
If I assign name = result.name. Then when I log of pass that value to a Django template, it displays as u'Dominion'
How do I format it to display as Dominion?
++ UPDATE ++
I think the problem has to do with printing values from a list or dictionary. For example:
result = freebase.mqlread(query)
games = {}
count = 0
r = result[0]
name = r.name
games["name"] = name,
self.response.out.write(games["name"])
self.response.out.write(name)
This displays as:
(u'Dominion',) // saved response to dictionary, and then printed
Dominion // when calling the value directly from the response
I need to iterate through an array of JSON items and the values are being shown with the unicode. Why?
The comma at the end of games["name"] = name, makes it a 1-tuple. Remove it.
>>> # example
>>> s = u"Jägermütze"
>>> s.encode("utf-8")
'J\xc3\xa4germ\xc3\xbctze'
>>> print s.encode("utf-8") # on a utf-8 terminal
Jägermütze
Don't know much about Django, but not accepting snicode strings seems unpythonic to me.
You can use str(your string) to do this.