python,用迭代改变字典值

时间:2016-05-31 14:44:29

标签: python

我有以下内容:

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",}

我做错了什么?

1 个答案:

答案 0 :(得分: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)