Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python请求以JSON格式U';添加到结果中_Python_Url_Unicode_Python Requests - Fatal编程技术网

Python请求以JSON格式U';添加到结果中

Python请求以JSON格式U';添加到结果中,python,url,unicode,python-requests,Python,Url,Unicode,Python Requests,我想从结果中删除u(unicode值)。其他工具没有以JSON格式读取它 import requests import json headers={ "accept": "application/json", "content-type": "application/json" } test_urls = ['https://google.com'] def return_json(url): try: response =

我想从结果中删除
u
(unicode值)。其他工具没有以JSON格式读取它

import requests
import json

headers={
        "accept": "application/json",
        "content-type": "application/json"
    }

test_urls = ['https://google.com']


def return_json(url):
    try:
        response = requests.get(url,headers=headers)

        # Consider any status other than 2xx an error
        if not response.status_code // 100 == 2:
            return "Error: Unexpected response {}".format(response)

        json_obj = response.json()
        return json_obj
    except requests.exceptions.RequestException as e:
        # A serious problem happened, like an SSLError or InvalidURL
        return "Error: {}".format(e)


for url in test_urls:

    print "Fetching URL '{}'".format(url)
    print return_json(url)
结果:

{u'rows': [{u'timestamp': 1585924500001L, u'RAM_': 1000, u'Allocated': 4000.78, u'queue': 0, u'Details':  u'Connected': 2, u'Queue': 0, u'EventsQueue': 0}]
我希望结果的值中不包含
u
您可以使用
encode()
方法将unicode字符串转换为ascii字符串,如下所示:

import requests
import json

headers={
        "accept": "application/json",
        "content-type": "application/json"
    }

test_urls = ['http://www.mocky.io/v2/5185415ba171ea3a00704eed']


def return_json(url):
    try:
        response = requests.get(url,headers=headers)

        # Consider any status other than 2xx an error
        if not response.status_code // 100 == 2:
            return "Error: Unexpected response {}".format(response)

        json_obj = response.json()
        return json_obj
    except requests.exceptions.RequestException as e:
        # A serious problem happened, like an SSLError or InvalidURL
        return "Error: {}".format(e)


for url in test_urls:

    print("Fetching URL '{0}'".format(url))
    ret = return_json(url)
    ret = {unicode(k).encode('ascii'): unicode(v).encode('ascii') for k, v in ret.iteritems()}
    print(ret)

请参阅并停止使用Python 2Unicode字符串是正确的字符串类型(当您想要的实际上是一个“字符串”,而不是一个串行化的字节序列以通过线路/进入文件/etc时)。这是一个特性,不是bug;你不应该试图改变它。只需切换到Python3,其中这些字符串在打印时前面没有
u
(而另一种类型——ByTestRing——在打印时前面有
b
)…也就是说,
u
s根本不在“值中”。它们只是描述数据类型的符号,数据类型是最好的。在查看链接问题的答案时,请记住,您可以使用
response.text
,访问响应的原始文本,从而将其直接传递给您想要的任何JSON解码逻辑。在中,请参阅“回答好的问题”一节,这里的要点是关于“以前已经被问过很多次了”的问题对不起,我是新来的。我会记住的。谢谢。@alefmim输出仍然包含U'unicode值。