Home > Mobile >  Find specific values in a txt file and adding them up with python
Find specific values in a txt file and adding them up with python

Time:02-12

I have a txt file which looks like that:

[Chapter.Title1]
Irrevelent=90 B
Volt=0.10 ienl
Watt=2 W 
Ampere=3 A 
Irrevelent=91 C

[Chapter.Title2]
Irrevelent=999
Irrevelent=999
    
[Chapter.Title3]
Irrevelent=92 B
Volt=0.20 ienl
Watt=5 W 
Ampere=6 A 
Irrevelent=93 C

What I want is that it catches "Title1" and the values "0,1", "2" and "3". Then adds them up (which would be 5.1).

I don't care about the lines with "irrevelent" at the beginning.

And then the same with the third block. Catching "Title3" and adding "0.2", "5" and "6".

The second block with "Title2" does not contain "Volt", Watt" and "Ampere" and is therefore not relevant.

Can anyone please help me out with this?

Thank you and cheers

CodePudding user response:

You can use regular expressions to get the values and the titles in lists, then use them.

txt = """[Chapter.Title1]
Irrevelent=90 B
Volt=1 V
Watt=2 W 
Ampere=3 A 
Irrevelent=91 C

[Chapter.Title2]
Irrevelent=92 B
Volt=4 V
Watt=5 W 
Ampere=6 A 
Irrevelent=93 C"""
#that's just the text


import re

rx1=r'Chapter.(.*?)\]'
rxv1=r'Volt=(\d )'
rxv2=r'Watt=(\d )'
rxv3=r'Ampere=(\d )'
res1 = re.findall(rx1, txt)
resv1 = re.findall(rxv1, txt)
resv2 = re.findall(rxv2, txt)
resv3 = re.findall(rxv3, txt)

print(res1)
print(resv1)
print(resv2)
print(resv3)

Here you get the titles and the interesting values you want :

['Title1', 'Title2']
['1', '4']
['2', '5']
['3', '6']

You can then use them as you want, for example :

for title_index in range(len(res1)):
  print(res1[title_index])
  value=int(resv1[title_index]) int(resv2[title_index]) int(resv3[title_index])  
  #use float() instead of int() if you have non integer values
  print("the value is:", value)

You get :

Title1
the value is: 6
Title2
the value is: 15

Or you can store them in a dictionary or an other structure, for example :

#dict(zip(keys, values)) 
data= dict(zip(res1, [int(resv1[i]) int(resv2[i]) int(resv3[i]) for i in range(len(res1))] ))

print(data)

You get :

{'Title1': 6, 'Title2': 15}

Edit : added opening of the file

import re

with open('filename.txt', 'r') as file:
    txt = file.read()


rx1=r'Chapter.(.*?)\]'
rxv1=r'Volt=([0-9] (?:\.[0-9] )?)'
rxv2=r'Watt=([0-9] (?:\.[0-9] )?)'
rxv3=r'Ampere=([0-9] (?:\.[0-9] )?)'
res1 = re.findall(rx1, txt)
resv1 = re.findall(rxv1, txt)
resv2 = re.findall(rxv2, txt)
resv3 = re.findall(rxv3, txt)

data= dict(zip(res1, [float(resv1[i]) float(resv2[i]) float(resv3[i]) for i in range(len(res1))] ))

print(data)

Edit 2 : ignoring missing values

import re

with open('filename.txt', 'r') as file:
    txt = file.read()
  
#divide the text into parts starting with "chapter"
substr = "Chapter"
chunks_idex = [_.start() for _ in re.finditer(substr, txt)]
chunks = [txt[chunks_idex[i]:chunks_idex[i 1]-1] for i in range(len(chunks_idex)-1)]
chunks.append(txt[chunks_idex[-1]:]) #add the last chunk
#print(chunks)

keys=[]
values=[]
rx1=r'Chapter.(.*?)\]'
rxv1=r'Volt=([0-9] (?:\.[0-9] )?)'
rxv2=r'Watt=([0-9] (?:\.[0-9] )?)'
rxv3=r'Ampere=([0-9] (?:\.[0-9] )?)'

for chunk in chunks:

  res1 = re.findall(rx1, chunk)
  resv1 = re.findall(rxv1, chunk)
  resv2 = re.findall(rxv2, chunk)
  resv3 = re.findall(rxv3, chunk)
  # check if we can find all of them by checking if the lists are not empty
  if res1 and resv1 and resv2 and resv3 :
    keys.append(res1[0])
    values.append(float(resv1[0]) float(resv2[0]) float(resv3[0]))

data= dict(zip(keys, values ))
print(data)

CodePudding user response:

Here's a quick and dirty way to do this, reading line by line, if the input file is predictable enough.

In the example I just print out the titles and the values; you can of course process them however you want.

f = open('file.dat','r')

for line in f.readlines():

    ## Catch the title of the line:
    if '[Chapter' in line:
        print(line[9:-2])

    ## catch the values of Volt, Watt, Amere parameters
    elif line[:4] in ['Volt','Watt','Ampe']:
        value = line[line.index('=') 1:line.index(' ')]
        print(value)

    ## if line is "Irrelevant", or blank, do nothing

f.close()

CodePudding user response:

There are many ways to achieve this. Here's one:

d = dict()
V = {'Volt', 'Watt', 'Ampere'}
with open('chapter.txt', encoding='utf-8') as f:
    key = None
    for line in f:
        if line.startswith('[Chapter'):
            d[key := line.strip()] = 0
        elif key and len(t := line.split('=')) > 1 and t[0] in V:
            d[key]  = float(t[1].split()[0])

for k, v in d.items():
    if v > 0:
        print(f'Total for {k} = {v}')

Output:

Total for [Chapter.Title1] = 6
Total for [Chapter.Title2] = 15
  • Related