蟒蛇,改变字典值与迭代
问题描述:
我有以下几点:蟒蛇,改变字典值与迭代
max_id = 10
for i in range(max_id):
payload = "{\"text\": 'R'+str(i),\"count\":\"1 \",}"
print(payload)
我想通过这个迭代,并有文字的值设置为“R1”,“R2” ......在调试输出是:
{"text": 'R'+str(i),"count":"1",}
我在做什么错?
答
for i in range(max_id):
payload = "{\"text\": R"+str(i)+",\"count\":\"1 \",}"
print(payload)
双引号问题。
输出:
{"text": R0,"count":"+i+ ",}
{"text": R1,"count":"+i+ ",}
{"text": R2,"count":"+i+ ",}
{"text": R3,"count":"+i+ ",}
{"text": R4,"count":"+i+ ",}
{"text": R5,"count":"+i+ ",}
{"text": R6,"count":"+i+ ",}
{"text": R7,"count":"+i+ ",}
{"text": R8,"count":"+i+ ",}
{"text": R9,"count":"+i+ ",}
我是你正在寻找这一个。
for i in range(10):
payload = "{\"text\": R%s,\"count\":\"1 \",}" %(i)
print(payload)
仔细检查双引号。在你的问题中的语法突出显示可能会给你一个提示。 –
[在python中的字符串插值](http://stackoverflow.com/questions/4450592/string-interpolation-in-python) –
基于http://stackoverflow.com/questions/4450592/string-interpolation-in-python ,我已经想出了payload =“{\”text \“:%s,\”count \“:\”1 \“}”%(“R”+ str(i)) - 谢谢 – user61629