0

I have a .txt file like this image for txt file, where every column can be classified as UserName:ReferenceToThePassowrd:UserID:GroupID:UserIDInfo:HomeDir:LoginShell. I want to make a script to parse the file and generate a list of dictionaries where each user information is represented by a single dictionary:

Here is an example of how the final output should look like:

[
    {
        "user_name": "user1",
        "user_id": "1001",
        "home_dir": "/home/user1",
        "login_shell": "/bin/bash"
    },
    {
        "user_name": "user2",
        "user_id": "1002",
        "home_dir": "/home/user2",
        "login_shell": "/bin/bash"
    }
]
Camilo Martinez M.
  • 1,420
  • 1
  • 7
  • 21
  • 2
    Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – Doga Oruc May 08 '21 at 02:38

1 Answers1

0

A very basic way of doing this might look like:

objects = []
for line in open("myData.txt"):
    line = line.strip()
    items = line.split(":")

    someDict = {}
    someDict["first"] = items[0]
    someDict["second"] = items[1]
    # ... etc
    objects.append(someDict)

print(objects)
Parad0x13
  • 2,007
  • 3
  • 23
  • 38