How to disregard keys inside a python formatted string

Asked

Viewed 92 times

0

Hello, this is my first question here, I have a string and in it I need for a random value, however, when making it formatted, other keys inside become a placeholder, but that’s not what I want.It’s this code: The string is about-if from a java script Function, and in it there are keys, there are as I "disregard these keys" in the format string?

import random
number = random.randint(280, 320)
func = f"""
function saveToLocalStorage(e) {
    var t = localStorage.getItem("readData2") || "[]",
        t = JSON.parse(t),
        n = {
            slug: e["viewed_book[book_slug]"],
            percentage: e["viewed_book[percentage]"],
            initial_percentage: e.initial_percentage,
            final_percentage: e.final_percentage,
            resume_element_id: e.resume_element_id,
            resume_doc_id: e.resume_doc_id,
            time_spent: {},
            viewed_book_id: gon.viewed_book,
            words_per_minute: e.words_per_minute,
            token: e.token
        },
        r = t.concat(n);
    localStorage.setItem("readData2", JSON.stringify(r))
}""".
print(func)
  • 2

    Just escape the keys by replacing the { for {{ and } for }}, but in your case I think it would be best to simply remove the f at the beginning of the string, since you are not using even interpolation.

  • Man, thank you so much! The use of two keys served me!

1 answer

0

How @user140828 posted as comment:

Just escape the keys by replacing the { por {{ e } by }}.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.