如何将代理列表转换为代理的json
我有一个代理列表:如何将代理列表转换为代理的json,json,python-3.x,Json,Python 3.x,我有一个代理列表: proxies = [ "188.166.78.120:8080", "18.210.69.172:3128", "178.128.166.50:8118" ] 但对于一个库,我需要这样的JSON: proxies = { "http": "http://188.166.78.120:8080", "https": "http://188.166.78.120:8080", "http": "http://18.210.69.172:3128",
proxies = [
"188.166.78.120:8080",
"18.210.69.172:3128",
"178.128.166.50:8118"
]
但对于一个库,我需要这样的JSON:
proxies = {
"http": "http://188.166.78.120:8080",
"https": "http://188.166.78.120:8080",
"http": "http://18.210.69.172:3128",
"https": "http://18.210.69.172:3128",
"http": "http://178.128.166.50:8118",
"https": "http://178.128.166.50:8118"
}
我怎样才能做到?
我试过这个,但我只得到最后一个
new = {}
for i in proxies:
proxy = {
"http":"http/"+i,
"https":"http/"+i
}
new.update(proxy)
因为您必须使用python字典的唯一键。对于多个值,您需要使用dict中的列表 下面是您问题的解决方案
import json
proxies = [
"188.166.78.120:8080",
"18.210.69.172:3128",
"178.128.166.50:8118"
]
js = dict()
for i in proxies:
js.setdefault('http',[])
js.setdefault('https',[])
js['http'].append('http://'+i)
js['https'].append('http://'+i)
x = json.dumps(js)
y = json.loads(x)
print(js)
print(x)
print(y["http"])
python字典:
{'http': ['http://188.166.78.120:8080',
'http://18.210.69.172:3128',
'http://178.128.166.50:8118'],
'https': ['http://188.166.78.120:8080',
'http://18.210.69.172:3128',
'http://178.128.166.50:8118']}
json:
http列表:
['http://188.166.78.120:8080', 'http://18.210.69.172:3128', 'http://178.128.166.50:8118']
正如其他用户所指出的,如果可以的话,您应该避免使用该格式,因为大多数JSON解析器会把它看作字典,因此密钥必须是唯一的。 下面是一种测试方法,创建一个格式正确的字符串,然后将其编码为JSON:
import json
proxies = [
"188.166.78.120:8080",
"18.210.69.172:3128",
"178.128.166.50:8118"
]
p = []
p.append('{')
for i, proxy in enumerate(proxies):
p.append('"http": "http://{0}", "https": "https://{0}"'.format(proxy))
if not i == len(proxies) - 1:
p.append(',')
p.append('}')
text_format = ''.join(p)
print (text_format)
输出是您要查找的格式:
{"http": "http://188.166.78.120:8080", "https": "https://188.166.78.120:8080","http": "http://18.210.69.172:3128", "https": "https://18.210.69.172:3128","http": "http://178.128.166.50:8118", "https": "https://178.128.166.50:8118"}
现在让我们将该字符串转换为JSON:
json_format = json.loads(text_format)
print (json_format)
只有第一个键存在,因为不允许有重复的键:
{'http': 'http://178.128.166.50:8118', 'https': 'https://178.128.166.50:8118'}
因此,您可以只使用上面生成的字符串,或者使用另一种有效的方式对其进行编码,例如:
import json
proxies = [
"188.166.78.120:8080",
"18.210.69.172:3128",
"178.128.166.50:8118"
]
p = [{'http': 'http://{0}'.format(item), 'https': 'https://{0}'.format(item)} for item in proxies]
j = json.dumps(p)
print (j)
输出:
[{"http": "http://188.166.78.120:8080", "https": "https://188.166.78.120:8080"}, {"http": "http://18.210.69.172:3128", "https": "https://18.210.69.172:3128"}, {"http": "http://178.128.166.50:8118", "https": "https://178.128.166.50:8118"}]
您的结果在技术上是有效的JSON,但大多数JSON解码器不会像您预期的那样对其进行解码;因为只有一个重复键中的值将被保留。你确定这是所需的输出格式吗?是的,我尝试了第一个,但第二个不起作用。它起作用了吗?你尝试过在列表上循环以正确的格式生成对象吗?是的,但我只得到列表中的最后一个条目。你能展示你的尝试吗?如果提问者显然已经尝试首先自己解决问题,那么问题会得到更多的回答。这与我需要的输出完全不同。这是不可能的way@SayedSohan事实并非如此。如果他们真的想要JSON,当然有可能产生JSON。我知道这是可能的。。但使用JSON无法获得预期的结果。
[{"http": "http://188.166.78.120:8080", "https": "https://188.166.78.120:8080"}, {"http": "http://18.210.69.172:3128", "https": "https://18.210.69.172:3128"}, {"http": "http://178.128.166.50:8118", "https": "https://178.128.166.50:8118"}]