在接口測試中,直接使用r.json()獲取到的結果,難免會在結果中帶有中文,但是在控制台的中文輸出默認是Unicode編碼,不能通過統一在設置中進行設置utf-8,因此為了在控制台更顯而易見的顯示出中文的提示,就有了如下的操作。
使用的編輯器:pycharm
直接打印:print r.json()
中文在控制台的顯示:

解決方案:
import json get_result = r.json() print json.dumps(get_result, encoding="utf-8", ensure_ascii=False)
打印結果:

詳細的參考代碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author:lucky,time:2019-06-11
import requests import json def test_login(): url = "https://*****.*****.com/v2/user/login" # 接口
url_Write_excel = url[url.rfind('/v2'):] # 獲取非域名外的url鏈接,最后寫入到Excel中
data = { "mobile": "12606666333", "password": "ee79976c9380d5e337fc1c095ece8c8f22f91f306ceeb161fa51fecede2c4ba1" } headers = { "version": "2.3.1", "version-id": "239", "device-id": "8BAFD18C-61E3-4BAF-8AB1-0BC16E567633", "time": "1560175716224", "channel-id": "001", "os": "ios", "Accept-Language": "zh-tw", "device-name": "iPhoneX", "User-Agent": "iBer/239 CFNetwork/902.2 Darwin/17.7.0", # 注:一定不能加content-type,否則報簽名錯誤
# Content-Type: multipart/form-data; boundary=vHsPJ5s6grMzpmWoZxd3T3o6.LcUWBvUUu0gDNubaf05Ve7kv6bAkH3s_rr0AEc2D6AbEh
"sign": "629bb0721dc60ff71725b40f46a3d1b5" } r = requests.post(url=url, data=data, headers=headers, timeout=100) get_result = r.json() print json.dumps(get_result, encoding="utf-8", ensure_ascii=False) test_login()
