0

The goal is to get my text file that contains 1 proxy per line, formatted as ip:port into a dictionary in my python script. Having a blast with python so far, but this issue is making me want to pull my hair out-- I've read many many questions but none of them seem to be quite what I'm looking for.

this is the what i'm currently working with:

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)

Any help greatly appreciated

ti7
  • 16,375
  • 6
  • 40
  • 68
wannabee
  • 3
  • 1
  • Hi @wannabee ! First off, a quick intro to dictionaries in Python. The basic is you have a list of key:value pairs. If you want see a value, you look it up by the key. Given that, could you edit your question to make it a little clearer what you want `addproxies` to achieve? My guess is you want to end up with proxies being a mapping from IPs to ports, something like `proxies["xxx.xxx.xxx.xxx"]` and you'd get back the port number; but just want to make sure I'm not writing an answer for the wrong question :) – Alecg_O Mar 31 '20 at 13:57
  • @Alecg_O Actually since asking I've realized dictionary isnt really the right way about going for what I'm after. I switched it so that i have a list named proxies containing a list of ips and a list of ports, so that i could pull both a proxy and a port from the list proxies. I wanted to make it work similar to an array in java but was just really confused about the python dictionary/list syntax and use cases. Thanks for the help-- I'm understanding more why it is important to write good questions! – wannabee Apr 01 '20 at 00:56

1 Answers1

0

Try this:

def main():
    proxies = {}
    with open('proxies.txt', 'r') as this_file:
        for line in this_file:
            ip, port = line.strip().split(":")
            proxies[ip] = port
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53
  • 1
    thanks a lot! didnt work the first time.... realized i was saving it to a different directory than the one i kept running... I wonder if i actually had it working at one point or another and didnt realize it smh. – wannabee Mar 31 '20 at 13:27