0

I have a text file that is very unorganized with multiple-sub-index objects, something like this:

1:
Name of Object 1

Sub-index 0: 
Scale: Q0
Unit: Percent
Description: Object 1 does this

2:
Object 2 yo

Sub-index 0: 
Scale: Q0
Unit: Percent
Description: Something important

Sub-index 1: 
Scale: 0.125
Unit: Percent
Description: Object 2 does that

I want to extract these objects' name, scale and description and make them into a dictionary. Something like this:

ObjectDict = {
    1: ['Name of Object 1', 'Q0', 'Object 1 does this'],
    2: {
        0: ['Object 2 yo', 'Q0', 'Something important'],
        1: ['Object 2 yo', '0.125', 'Object 2 does that']
    }
}

I was able to extract the dictionary keys by doing this:

for line in textfile:
    a = line.replace(':', '')
    if b.isnumeric():
        # this is 1 key

I can "probably" extract Scale and Description value of an object by doing:

if 'Scale' in line: # Store the value
if 'Description' in line: # Store the value

However, this would only work if the object only has 1 sub-index. For multiple-sub-index objects like Object 2, I could not figure out how to do them yet. Is there a nice way to do this in Python 3.7? Thanks!

EDIT: The dictionary format I chose above is just an example. Any other formatted dictionary is okay. I just want to extract necessary data from an unorganized file and store it more properly so other files can access them.

5
  • 1
    Two comments on your choice of data structure: first, I would not recommend have the structure of each element depend on its contents. In your example, having the value for 1 be a list but the value for 2 be a dict doesn't sound like a good idea. Just save a dict with one item. Second, indexing dicts with integers is rarely justified. If that's your usecase, just use lists. Commented Dec 23, 2021 at 18:26
  • 2
    Your proposed dictionary is far from ideal. You're duplicating information unnecessarily (the object name) and you're losing information (the scale/unit/description property names). Commented Dec 23, 2021 at 18:27
  • Regarding you question in particular, I'd branch if 'Sub-index' in line and then just do what you said. But think again about your chosen structure. Commented Dec 23, 2021 at 18:29
  • My bad on this. The dictionary I show here is just for example. Any other formatted dictionary should work. Commented Dec 23, 2021 at 18:29
  • Please read my added "Edit" comment in the post for more details Commented Dec 23, 2021 at 18:32

1 Answer 1

1

If you use dictionaries for every object in txt file you can loop through lines of txt file and use some of python builtin functions like readlines() and startswith() to do what you want.

f = open('sample.txt')
lines = f.readlines()
d = {}
for i,line in enumerate(lines):
    if line[:-2].isnumeric():
        ind =  line[:-2]
        name = lines[i+1].replace('\n', '')
        if not ind in d:
            d[ind] = {}

    if line.startswith('Sub-index'):
        sub_ind = line.split()[-1].split(':')[0]
        if not sub_ind in d[ind]:
            d[ind][sub_ind] = []
            d[ind][sub_ind].append(name)

    if line.startswith('Scale'):
        scale = line.split()[-1]
        d[ind][sub_ind].append(scale)

    if line.startswith('Description'):
        desc = line.split(': ')[-1].replace('\n', '')
        d[ind][sub_ind].append(desc)

Output:

{
    '1': {
        '0': ['Name of Object 1', 'Q0', 'Object 1 does this']
        },
    '2': {
        '0': ['Object 2 yo', 'Q0', 'Something important'],
        '1': ['Object 2 yo', '0.125', 'Object 2 does that']
        }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That has done the trick. After running it however, I figured that the file has a few objects that does not have "Sub-index" and a "Scale", (so only Name and a Description), so that object's dictionary is empty. I want to add in Sub-index 0 and Scale of 1 for them. Is there a nice way to do that?
Maybe it is better to have a dictionary for every sub-object instead of list. Then dictionary keys would be [sub-index, name, scale, description]. So at the end of loop, if any of keys has no value you can add what you want to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.