[英]Python Add Delimiter to Variable
So I have a HTTP POST from a device to my flask server, that sends the following message: 所以我有一个从设备到我的烧瓶服务器的HTTP POST,它发送以下消息:
dump= '{"on":"false"}'
r = requests.post('http://127.0.0.1:5000', data=dump,
headers={'Content-Type': 'application/json'})
From the Flask server, I then try to PUT to another device on my network: 然后,我从Flask服务器尝试将其放置到网络上的另一台设备上:
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def signal():
if request.method == 'POST':
content = request.get_json()
print(content)
r = requests.put("http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights/5/state/", content)
return jsonify(content)
else:
return 'Hello, world!'
if __name__ == '__main__':
app.run(debug=True)
The only problem is the apostrophe delimiter is removed after I send the first HTTP Request. 唯一的问题是我发送第一个HTTP请求后,撇号分隔符被删除。 How could I get add a delimiter around a variable? 我如何在变量周围添加定界符?
Use something like this: 使用这样的东西:
content = "'" + str(request.get_json()) "'"
or you can also try the str.format
way: 或者您也可以尝试使用str.format
方法:
content = "'{}'".format(str(request.get_json()))
Hope it helps. 希望能帮助到你。
EDIT: 编辑:
Converted the returned dict
of request.get_json() to str
before formatting. 格式化之前将返回的request.get_json() dict
转换为str
。
Not sure why you need to send a quoted string. 不知道为什么需要发送带引号的字符串。
Maybe one of these helps. 也许这些帮助之一。
In [1]: d = {"on":"false"}
In [2]: repr(d)
Out[2]: "{'on': 'false'}"
In [3]: import json
In [4]: s = json.dumps(d)
In [5]: str(s)
Out[5]: '{"on": "false"}'
In [6]: repr(s)
Out[6]: '\'{"on": "false"}\''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.