Cflan99
Cflan99

Reputation: 29

Writing a python dictionary from a text file with an unusual format

I am trying to creat a python dictionary from a text file that is formatted in this way. I cannot change it.

dataname
definition of data
dataname2
definition of data2
dataname3
definition of data3

The data is routinely unspaced. The keys have to be the the dataname and the values have to be the definition of the data. I know I need readlines() or to keep track of which line is a key and which is a value somehow. This is what I have. Any help is appreciated.

d = {}
with open('dict.txt', 'r') as myFile:
    for line in myFile:
        x = myFile.readlines()
        word = x[0]
        description = x[1]
        d[word] = description
    print(d)

Upvotes: 1

Views: 87

Answers (5)

dawg
dawg

Reputation: 104062

You can use zip_longest from itertools to do:

import itertools
ur_dict={}
with open(ur_file) as f:
    for line1,line2 in itertools.zip_longest(*[f]*2):
        ur_dict[line1.rstrip()]=line2.rstrip()

>>> ur_dict
{'dataname': 'definition of data', 'dataname2': 'definition of data2', 'dataname3': 'definition of data3'}

Which could be a dict comprehension:

with open('so.txt') as f:
    ur_dict={line1.rstrip():line2.rstrip() 
            for line1,line2 in itertools.zip_longest(*[f]*2)}

Or, you can use zip with the iterator twice:

with open(ur_file) as f:
    ur_dict={l1.rstrip():l2.rstrip() for l1,l2 in zip(f,f)}

Upvotes: 0

Prashant Kumar
Prashant Kumar

Reputation: 2092

I would do it like below.

  1. Read all the lines into a list with each line being a separate element in list.
  2. Create a sub_list out of main list to be considered as keys. This would be alternate elements from main list starting from element 0.
  3. Create a sub_list out of main list to be considered as values. This would be alternate elements from main list starting from element 1.
  4. Merge the 2 lists so that 1st element from key_list is mapped with 1st element of value_list.

Try this :

with open('dict.txt') as f:
    content = f.readlines()

content = [x.strip() for x in content]   # to remove whitespace characters like `\n` at the end of each line

key_list = content[::2]       # every alternate element in list is treated as keys. Starting from 1st element.
value_list = content[1::2]    # every alternate element in list is treated as value. Starting from 2nd element.

final_dict = dict(zip(key_list, value_list))    # merge the 2 lists to create a key, value pair dictionary.

print(final_dict)

Upvotes: 0

felipe
felipe

Reputation: 8045

with open('dict.txt', 'r') as file:
    split = file.read().splitlines()
    dictionary = {k:v for k, v in zip(split[::2], split[1::2])}
{'dataname': 'definition of data',
 'dataname2': 'definition of data2',
 'dataname3': 'definition of data3'}

print(file.splitlines()[::2]) 
# >>> ['dataname', 'dataname2', 'dataname3']

print(file.splitlines()[1::2])
# >>> ['definition of data', 'definition of data2', 'definition of data3']

Use slice(start, stop, step) operation to give you odd and even list items.

Upvotes: 0

Littin Rajan
Littin Rajan

Reputation: 897

Code:

d = {}
file = open('dict.txt', 'r')
text = file.read().strip()
file.close()

textlist = text.split('\n')
for i in range(0, len(textlist), 2):
    colname = textlist[i]
    colvalue = [textlist[i+1]]
    d[colname] = colvalue
#display dictionary
print(d)
df = pd.DataFrame(d)
#display dataframe 
print(df)

I hope it would be helpful.

Upvotes: 0

Thierry Lathuille
Thierry Lathuille

Reputation: 24280

You can iterate on the file with a for loop, which will iterate on the lines, and use the next method of the file object to iterate to the next line:

d = {}

with open('dict.txt') as f:
    for line in f:
        name = line.strip()
        description = next(f).strip()
        d[name] = description

print(d)
#{'dataname': 'definition of data', 'dataname2': 'definition of data2', 'dataname3': 'definition of data3'}

Upvotes: 1

Related Questions