Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select specific value for curl brace in f-string

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'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"]'
})

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading