-1

I want to pass the value "How are you?" of question to body so body has the value as shown below.

question = "How are you?"
body = r’{
    "uid": "xxxx’,
    "msg": {
        "msgType": "text",
        "msgBody": {
            "content": "How are you"
        }
    }
}

I tried replacing r with fr and placing the curly braces deep inside but did not seem to work. The purpose I am doing this because I need to hash the string so I can include the hashed string in a URL.

accdias
  • 5,160
  • 3
  • 19
  • 31
James Chang
  • 608
  • 8
  • 21
  • 3
    Are you trying to build a JSON string? In that case, using the `json` module from the standard library might save you some headaches. – Ture Pålsson Apr 08 '21 at 08:14
  • 4
    These `’“”’` are not valid quote characters. That's the most obvious reason your code would not work. – khelwood Apr 08 '21 at 08:16
  • @khelwood The body variable can be printed in Python as expected. – James Chang Apr 08 '21 at 08:22
  • @JamesChang Not from the code you have posted, it can't. – khelwood Apr 08 '21 at 08:23
  • @khelwood Sorry, I guess it’s probably I am typing the question on my iPad. I am running the code on a laptop which is denied access to Stackoverflow. – James Chang Apr 08 '21 at 08:25
  • Does this answer your question? [Python3 f-strings: how to avoid having to escape literal curly brackets?](https://stackoverflow.com/questions/63315003/python3-f-strings-how-to-avoid-having-to-escape-literal-curly-brackets) – MisterMiyagi Apr 08 '21 at 08:26
  • @MisterMiyagi Unfortunately no. I’ve read this post. Mine is actually different because there is r-string involved. – James Chang Apr 08 '21 at 08:28
  • `body = rf'{{"uid": "xxxx", "msg": {{"msgType": "text", "msgBody": {{"content": "{question}"}}}}}}'`? – L3viathan Apr 08 '21 at 08:38
  • @L3viathan It worked! If you don’t mind, you can write down your answer with a little bit of explanation so more people would benefit. Thank you! – James Chang Apr 08 '21 at 08:42
  • 3
    Okay, but that example is simply doing the escaping of literal curly brackets... that OP described... in the question that you said you read and didn't find helpful. – Karl Knechtel Apr 08 '21 at 09:06
  • 1
    "Mine is actually different because there is r-string involved." How does this make a difference? You can combine *any* string literal with an f-string literal, including r-string literals. (FWIW, your example doesn't actually need to be an r-string.) – MisterMiyagi Apr 08 '21 at 10:03
  • @MisterMiyagi I see your point. Thanks! – James Chang Apr 09 '21 at 02:42

1 Answers1

2

There is a more Pythonic way to achieve what you want by using the json module and a dictionary. Something like this:

>>> import json
>>> question = 'How are you'
>>> body = {'uid': 'xxx', 'msg': {'msgType': 'text', 'msgbody': {'content': question}}}
>>> json.dumps(body)
'{"uid": "xxx", "msg": {"msgType": "text", "msgbody": {"content": "How are you"}}}'

To remove all the spaces after the separators, just pass the separators argument to json.dumps() like this:

>>> json.dumps(body, separators=(',', ':'))
'{"uid":"xxx","msg":{"msgType":"text","msgbody":{"content":"How are you"}}}'
accdias
  • 5,160
  • 3
  • 19
  • 31
  • Thank you for your reply. This method does not generate the same hashed string as the one proposed in the comment section. The product of `json.dumps()` leaves several spaces. I tried removing the spaces where you initialized `body` but nothing changed. – James Chang Apr 08 '21 at 09:40
  • 1
    Just pass `separators=(',', ':')` to `json.dumps()`. – accdias Apr 08 '21 at 14:10
  • 1
    I would add `ensure_ascii=False’ in `json.dumps()` to make it suit my purpose but thank you. This should really be the best solution! – James Chang Apr 09 '21 at 02:41
  • My pleasure. I'm glad to help. – accdias Apr 09 '21 at 15:32