-2

How can I read a text file containing

example:

qtsdatacenter.aws.edu 128.60.3.2 A

www.ibm.com 64.42.3.4 A

www.google.edu 8.7.45.2 A

Into a Dictionary like this:

 tsdictionary = {   "qtsdatacenter.aws.com" : "qtsdatacenter.aws.edu 128.60.3.2 A",     
                     "www.ibm.com" : "www.ibm.com 64.42.3.4 A",
                     "www.google.edu" : "www.google.edu 8.7.45.2 A"
                }

Where the first string read before a space also becomes the key.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
user586327
  • 5
  • 1
  • 3

2 Answers2

0
rsdictionary = {}
with open("PROJI-DNSRS.txt") as f:
    for line in f:
        (key, val) = line.strip().split(" ", 1)
        rsdictionary[key] = "{} {}".format(key, val)
jawad-khan
  • 313
  • 1
  • 10
0

You can achieve something like this with the following:

my_dict = dict()

with open("data.txt") as data:
  for line in data:
    site , *_= line.strip().split(" ", 1)
    if site:
      my_dict[site] = line.strip()

print(my_dict)
David Culbreth
  • 2,610
  • 16
  • 26