Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to turn read data from text file and turn into List of dictionary [{}, {}], output not expected

I have a text file "users.txt"

with the following data

admin|123|admin
user|123|user

I want to read all this List of dictionaries like [{}, {}, {} ]. Here is my approach, but I didn’t get it to work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def read(file, mode, dictionary_keys, split_char):
    read_lists = []
    read_dict = {}

    try:
        f = open(file, mode)
    except FileNotFoundError as e:
        print(e)
        return False

    else:
        for i in f.readlines():
            for j in range(len(dictionary_keys)):
                key = dictionary_keys[j]
                value = i.strip().split(split_char)[j]

                read_dict[key] = value

            read_lists.append(read_dict)

        return read_lists

Calling this method would be

user_dict = ['username', 'password', 'role']
a = read('users.txt', 'r', user_dict, '|')

print(a)

and my output which is not expected

[{'username': 'user', 'password': '123', 'role': 'user'}, {'username': 'user', 'password': '123', 'role': 'user'}]

Expected Output

[{'username': 'admin', 'password': '123', 'role': 'admin'}, {'username': 'user', 'password': '123', 'role': 'user'}]

>Solution :

Your problem is that when you append read_dict to read_lists, you keep appending a reference to the same dictionary, so your output list contains all copies of the last values written to the dictionary. You need to re-initialise read_dict for every line in the file. For example:

def read(file, mode, dictionary_keys, split_char):
    read_lists = []
    try:
        f = open(file, mode)
    except FileNotFoundError as e:
        print(e)
        return False
    else:
        for i in f.readlines():
            read_dict = {}
            for j in range(len(dictionary_keys)):
                key = dictionary_keys[j]
                value = i.strip().split(split_char)[j-1]
                read_dict[key] = value
            read_lists.append(read_dict)
        return read_lists

For your sample data this gives an output of

[
 {'username': 'admin', 'password': 'admin', 'role': '123'},
 {'username': 'user', 'password': 'user', 'role': '123'}
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading