Steve
Steve

Reputation: 83

How can I add a file of proxies into dictionary format

I'm trying to make my result something like:

proxies_dict = {
   'http':'http://178.141.249.246:8081',
   'http':'http://103.12.198.54:8080',
   'http':'http://23.97.173.57:80',
}

I have tried doing

proxies_dict = {}

with open('proxies.txt', 'r') as proxy_file:
    for proxy in proxy_file:
        proxies_dict['http'] = 'http://' + proxy.rstrip()

print(proxies_dict)

But this will only add the last line of proxy, not the whole thing. How can I make it add every proxy in a my .txt file?

Upvotes: 0

Views: 525

Answers (1)

Iris D
Iris D

Reputation: 72

Something like this could get ya going!

proxy text file looks like this:

178.141.249.246:8081
103.12.198.54:8080
23.97.173.57:80

proxies_list = []


with open('proxies.txt', 'r+') as proxy_file:
    
    # read txt file 
    proxies = proxy_file.readlines()
    
    for proxy in proxies:

        # initialize dict in loop 
        proxies_dict = {}

        # add proxy to dict 
        proxies_dict['http'] = 'http://' + proxy.rstrip()

        # append dict to list 
        proxies_list.append(proxies_dict)

print(proxies_dict)

[{'http': 'http://178.141.249.246:8081'},
 {'http': 'http://103.12.198.54:8080'},
 {'http': 'http://23.97.173.57:80'}]

Basically you have to read the file first and then when you add the item to the dictionary you append it to the list which will hold each of your proxies. I did it this way so you could keep the 'http' key for each of your proxies.

EDIT

If you need them all in one dictionary it would look something link this, per David's answer:

with open(file, 'r+') as f: 
    
    # read file 
    content = f.readlines()
 
    # this is an extra step for if you want to 
    # strip the newlines from each item, else 
    # links = content 
    # will work as well 
  
    links = [row.strip() for row in content] 
    
    # initialize dict 
    tmp = {}
    
    # create http key and and links list 
    tmp['http'] = links 
    
    # print result
    print(tmp)

{'http': ['178.141.249.246:8081', '103.12.198.54:8080', '23.97.173.57:80']}

Upvotes: 0

Related Questions