python+requests中data的写法

错误写法

data ={
            "anchor_user_id": 922442,
            "token": "4a4fdd9f125aa594bb1024c2ce12e404",
             "user_id": 15896319
        }
 接口报错1007   print data发现双引号变单引号

python+requests中data的写法_第1张图片

解法一:data用’’’ ‘’'包围


```python
data ='''{
    "anchor_user_id": 922442,
    "token": "4a4fdd9f125aa594bb1024c2ce12e404",
     "user_id": 15896319
}'''

python+requests中data的写法_第2张图片
解法二:’’’{}’’'中需要传变量时,
变量string,则%s
变量int,则%d


```python
ts = calendar.timegm(time.gmtime())
vid = xxxx
data ='''{
    "time": %s,
     "visitor_id": %d
}'''%(ts,vid)

解法三:json.dumps(data)

  ts = calendar.timegm(time.gmtime())
  vid = xxxx
  data = {
            "anchor_user_id": 922442,
            "time": ts,
            "visitor_id": vid
        }
        json_str=json.dumps(data)

你可能感兴趣的:(python,软件测试)