首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在txt文件中找到特定值,并将它们添加到python中。

在txt文件中找到特定值,并将它们添加到python中。
EN

Stack Overflow用户
提问于 2022-02-11 11:39:05
回答 3查看 236关注 0票数 1

我有一个txt文件,看起来是这样的:

代码语言:javascript
复制
[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

我想要的是它捕获"Title1“和值"0,1","2”和"3“。然后把它们加起来(即5.1)。

我不在乎开头写着“愤怒”的台词。

第三个街区也是如此。捕捉"Title3“并添加"0.2”、"5“和"6”。

带有"Title2“的第二个区块不包含"Volt”、Watt和"Ampere“,因此与此无关。

有人能帮帮我吗?

谢谢你,干杯

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-02-11 12:23:56

您可以使用正则表达式获取列表中的值和标题,然后使用它们。

代码语言:javascript
复制
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)

在这里,您可以得到您想要的标题和有趣的值:

代码语言:javascript
复制
['Title1', 'Title2']
['1', '4']
['2', '5']
['3', '6']

然后,您可以根据需要使用它们,例如:

代码语言:javascript
复制
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)

你得到:

代码语言:javascript
复制
Title1
the value is: 6
Title2
the value is: 15

或者可以将它们存储在字典或其他结构中,例如:

代码语言:javascript
复制
#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)

你得到:

代码语言:javascript
复制
{'Title1': 6, 'Title2': 15}

编辑:添加文件的打开

代码语言:javascript
复制
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)

编辑2:忽略缺失的值

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2022-02-11 12:34:18

如果输入文件足够可预测,下面是一种快速和肮脏的方法,可以逐行读取。

在这个例子中,我只是打印标题和值;当然,您可以任意处理它们。

代码语言:javascript
复制
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()
票数 0
EN

Stack Overflow用户

发布于 2022-02-11 12:53:29

要做到这一点,有很多方法。这里有一个:

代码语言:javascript
复制
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}')

输出:

代码语言:javascript
复制
Total for [Chapter.Title1] = 6
Total for [Chapter.Title2] = 15
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71079505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档