首页 > 解决方案 > 从 .txt 将代理添加到 python 中的字典

问题描述

目标是让我的文本文件每行包含 1 个代理,ip:port在我的 python 脚本中格式化为字典。到目前为止,我对 python 很感兴趣,但这个问题让我想把头发拉出来——我读过很多问题,但似乎没有一个是我想要的。

这是我目前正在使用的:

proxies = {}

def addproxies(a,b,proxies):
    proxies[ip].append(a),
    proxies[port].append(b)
    return proxies


def main():
    with open('proxies.txt', 'r') as this_file:
        for line in this_file:
            addproxies((this_file.split(':'),[0]),(this_file.split(':')[1]),proxies)

非常感谢任何帮助

标签: pythondictionaryfile-processing

解决方案


试试这个:

def main():
    proxies = {}
    with open('proxies.txt', 'r') as this_file:
        for line in this_file:
            ip, port = line.strip().split(":")
            proxies[ip] = port

推荐阅读