I have the following f-string:
test = [51, 138]
for i in test:
data = {
'ajax': '{"tab_id":"t0","cur_page":1,"cur_trie":"distance","query":"","critere":{"id_marque":[f"{{str(i)}}"]},"sliders":{"prix":{"id":"#range_prix","face":"prix","max_counter":997748,"min":"1","max":"400000"},"km":{"id":"#range_km","face":"km","max_counter":1098088,"min":"1","max":"500000"},"millesime":{"id":"#range_millesime","face":"millesime","max_counter":1179169,"min":"1910","max":"2022"}},"req_num":2,"nb_results":"11964250","current_location_distance":-1,"logged_in":false}',
'tabs': '["t0"]'
}
and I want to index the values as string from the list test in the list of values assigned to critere. However, I cannot properly index these values as the entire dictionary is a string, and there are multiple curly braces involved. How would I approach a situation like this?
The result I get it:
{'ajax': '{"tab_id":"t0","cur_page":1,"cur_trie":"distance","query":"","critere":{"id_marque":[f"{{str(i)}}"]},"sliders":{"prix":{"id":"#range_prix","face":"prix","max_counter":997748,"min":"1","max":"400000"},"km":{"id":"#range_km","face":"km","max_counter":1098088,"min":"1","max":"500000"},"millesime":{"id":"#range_millesime","face":"millesime","max_counter":1179169,"min":"1910","max":"2022"}},"req_num":2,"nb_results":"11964250","current_location_distance":-1,"logged_in":false}', 'tabs': '["t0"]'}
{'ajax': '{"tab_id":"t0","cur_page":1,"cur_trie":"distance","query":"","critere":{"id_marque":[f"{{str(i)}}"]},"sliders":{"prix":{"id":"#range_prix","face":"prix","max_counter":997748,"min":"1","max":"400000"},"km":{"id":"#range_km","face":"km","max_counter":1098088,"min":"1","max":"500000"},"millesime":{"id":"#range_millesime","face":"millesime","max_counter":1179169,"min":"1910","max":"2022"}},"req_num":2,"nb_results":"11964250","current_location_distance":-1,"logged_in":false}', 'tabs': '["t0"]'}
The expected output should be:
{'ajax': '{"tab_id":"t0","cur_page":1,"cur_trie":"distance","query":"","critere":{"id_marque":["51"]},"sliders":{"prix":{"id":"#range_prix","face":"prix","max_counter":997748,"min":"1","max":"400000"},"km":{"id":"#range_km","face":"km","max_counter":1098088,"min":"1","max":"500000"},"millesime":{"id":"#range_millesime","face":"millesime","max_counter":1179169,"min":"1910","max":"2022"}},"req_num":2,"nb_results":"11964250","current_location_distance":-1,"logged_in":false}', 'tabs': '["t0"]'}
{'ajax': '{"tab_id":"t0","cur_page":1,"cur_trie":"distance","query":"","critere":{"id_marque":["138"]},"sliders":{"prix":{"id":"#range_prix","face":"prix","max_counter":997748,"min":"1","max":"400000"},"km":{"id":"#range_km","face":"km","max_counter":1098088,"min":"1","max":"500000"},"millesime":{"id":"#range_millesime","face":"millesime","max_counter":1179169,"min":"1910","max":"2022"}},"req_num":2,"nb_results":"11964250","current_location_distance":-1,"logged_in":false}', 'tabs': '["t0"]'}
@Mozways answer is the solution I'm looking for; alternatively, here's the example requested in the comment:
#following with your example
test = [51, 138]
another_test = [5, 10]
data = []
for i, j in zip(test, another_test):
data.append({
'ajax': '{"tab_id":...,"critere":{"id_marque":[%s], "id_color":[????]},"sliders":...}' % i, # how to implement j here also for id_color
'tabs': '["t0"]'
})
CodePudding user response:
If you really wanted to use a f-string, the whole string should be f'…'. Adding 'f"xxx"' in the middle of the string does not make it a f-string.
The issue here with f-strings is that it requires curly brackets, which would require you to heavily escape all the curly brackets of the JSON: something like f'{{"critere":{{"id_marque":[{i}]}}}}.
A simple way might be to use classical python string formatting: "abc%sefg" % "c".
Also, you need to use a list to store your dictionary or you will overwrite the dictionary again and again with the last loop element:
test = [51, 138]
data = []
for i in test:
data.append({
'ajax': '{"tab_id":...,"critere":{"id_marque":[%s]},"sliders":...}' % i,
'tabs': '["t0"]'
})
output:
[{'ajax': '{"tab_id":...,"critere":{"id_marque":[51]},"sliders":...}',
'tabs': '["t0"]'},
{'ajax': '{"tab_id":...,"critere":{"id_marque":[138]},"sliders":...}',
'tabs': '["t0"]'}]
several parameters:
test = [51, 138]
another_test = [5, 10]
data = []
for params in zip(test, another_test): # params should be in order
data.append({
'ajax': '{"tab_id":...,"critere":{"id_marque":[%s], "id_color":["%s"]},"sliders":...}' % params,
'tabs': '["t0"]'
})
output:
[{'ajax': '{"tab_id":...,"critere":{"id_marque":[51], "id_color":["5"]},"sliders":...}',
'tabs': '["t0"]'},
{'ajax': '{"tab_id":...,"critere":{"id_marque":[138], "id_color":["10"]},"sliders":...}',
'tabs': '["t0"]'}]
NB. if you need a more complex format, or variable order of the parameters, then a f-string with quoting is more appropriate
CodePudding user response:
You can use a dictionary and then set the value directly and the use json.dumps to format result.
import json
test = [51, 138]
ajax = {
"tab_id": "t0",
"cur_page": 1,
"cur_trie": "distance",
"query": "",
"critere": {
"id_marque": [
None
]
},
"sliders": {
"prix": {
"id": "#range_prix",
"face": "prix",
"max_counter": 997748,
"min": "1",
"max": "400000"
},
"km": {
"id": "#range_km",
"face": "km",
"max_counter": 1098088,
"min": "1",
"max": "500000"
},
"millesime": {
"id": "#range_millesime",
"face": "millesime",
"max_counter": 1179169,
"min": "1910",
"max": "2022"
}
},
"req_num": 2,
"nb_results": "11964250",
"current_location_distance": -1,
"logged_in": False
}
for i in test:
ajax["critere"]["id_marque"] = [i]
data = {
'ajax': json.dumps(ajax),
'tabs': '["t0"]'
}
print(data)
CodePudding user response:
This would yield a list of the two dictionaries with the requested values:
test = [51, 138]
data = list(
{'ajax': '{{"tab_id":"t0","cur_page":1,"cur_trie":"distance","query":"","critere":{{"id_marque":["{0}"]}},"sliders":{{"prix":{{"id":"#range_prix","face":"prix","max_counter":997748,"min":"1","max":"400000"}},"km":{{"id":"#range_km","face":"km","max_counter":1098088,"min":"1","max":"500000"}},"millesime":{{"id":"#range_millesime","face":"millesime","max_counter":1179169,"min":"1910","max":"2022"}},"req_num":2,"nb_results":"11964250","current_location_distance":-1,"logged_in":false}}'.format(i),
'tabs': '["t0"]'}
for i in test)
