Home > Blockchain >  Reading lines from a document and storing certain lines in a dictionary/array - python
Reading lines from a document and storing certain lines in a dictionary/array - python

Time:08-07

I have a file I am reading lines from. using python 3.7

The file is automated, so the format of the file looks like this:


    Computer #1 - gaming 
       spec 1 = something 
       spec 2 = something 
       spec 3 = something
       other stuff = sadas 
    computer #2 - coding 
       spec 1 = something 
       spec 2 = something 
       spec 3 = something 
       other stuff = sadas
    computer #n - etc 
       spec 1 = something 
       spec 2 = something 
       spec 3 = something 
       other stuff = sadas

I want the info to be stored into a dictionar/array:

data = { "computer #1": [sepc1,spec2,spec3],"computer #2": [sepc1,spec2,spec3], "computer #n": [sepc1,spec2,spec3] }

lines = file_read.readlines()
for line in lines: 
    if 'computer #' in line: 
    data[line] = []
if 'spec1' in line:
    # here I want to add that value/line to the the first key/value such that: 
    # data['computer #1'].append(line)
    # data['computer #2'].append(line)
    # data['computer #n'].append(line)

the file I have is big and has relevant information. The lines above are just to illustrate what I am dealing with

I tried adding a counter every time we find the key word (computer #). but I wasn't sure how to check the status of the counter change so that I can add all of the "found/needed" lines (specs) into the array.

spent too much time trying to solve this. Thanks for the help.

CodePudding user response:

If spec is likely to occur in lines with "other stuff" (as part of a string or another word), it might be safer to use regex to match against the spec \d = pattern:

import re
data = {}

with open("file.txt", 'r') as f:
    for l in f.readlines():
        if 'computer #' in l:
            key = l.split('-')[0].strip()
            data[key] = []
        elif re.search(r'spec \d  =', l):
            val = l.split('=')[0].strip()
            data[key].append(val)

print(data)

Output:

{'computer #1': ['spec 1', 'spec 2', 'spec 3'], 'computer #2': ['spec 1', 'spec 2', 'spec 3'], 'computer #n': ['spec 1', 'spec 2', 'spec 3']}
  • Related