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

Time:09-15

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.

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'}]

CodePudding user response:

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'}
]

CodePudding user response:

It is so much easier to use pandas and it will give you the result in less code

import pandas as pd
df = pd.read_csv('data.csv',sep='|',dtype=str)
df.columns = [['username','password','role']]
dict_list = []
    
for i,row in df.iterrows():
    dict_list.append(row.to_dict())
  • Related