简体   繁体   English

从包含特定字母的字符串创建字典

[英]Creating dictionary from strings containing a specific letter

I'm trying to create a dictionary from a text file that contains test results.我正在尝试从包含测试结果的文本文件中创建字典。

The text file looks like this:文本文件如下所示:

NETAPP-MIB::enclTempSensorsCurrentTemp.1 = STRING: 29C (84F) ambient, 36C (96F), 36C (96F), 36C (96F), 36C (96F), 36C (96F), 57C (134F), 37C (98F), 57C (134F), 44C (111F), 59C (138F), 40C (104F), 45C (113F), 58C (136F), 42C (107F)

My goal is to get all the results that contain a number with the letter C.我的目标是获得所有包含字母 C 的数字的结果。 But I manage to get only the first value但我设法只得到第一个值

For example this is what I get:例如,这就是我得到的:

{'TEST sensor num 0': '29C'}

This is my code:这是我的代码:

import re

def get_names(ip):
    """Get the server name according the IP address"""
    names = {"TEST": "10.205.110.226", "TEST2": "10.205.111.216"}
    if ip in names.values():
        for key, val in names.items():
            if ip == val:
                return key
    else:
        return f"No Recognized unit {ip}"


def get_temps_servers():
    """ Return A dict mapping from sensor name to sensor value """
    result = {}
    count = 0
    with open("test.txt", "r") as newdata:
        text = newdata.read()
    for ip in online_server:
        name = get_names(ip)
        for c in re.findall(r"^.*?(\d+C)", text, flags=re.M):
            result[f"{name} sensor num {count}"] = c
            count = count + 1
    print(result)
    return result


global online_netapp
online_server = ["10.205.110.226"]

get_temps_servers()

What I want to get is result like this example:我想要得到的是像这个例子这样的结果:

{'TEST sensor num 0': '29C', 'TEST sensor num 1': '36C', 'TEST sensor num 2': '36C'}

And will continue like this as long as there is a number with C.只要有 C 的数字,就会继续这样。

What am I doing wrong?我究竟做错了什么?

your regex is not correct, it only look for only upto first number (29c) match.您的正则表达式不正确,它只查找最多第一个数字 (29c) 匹配。 Check it here https://regex101.com/r/sGVyac/1在这里查看https://regex101.com/r/sGVyac/1

Try excluding the caret like this (\d+C) , https://regex101.com/r/A40nuu/1尝试像这样排除插入符号(\d+C)https://regex101.com/r/A40nuu/1

Maybe this will nudge you in the right direction:也许这会推动你朝着正确的方向前进:

import json
import re

pattern = re.compile(r"\d{2}C")
long_string = "NETAPP-MIB::enclTempSensorsCurrentTemp.1 = STRING: 29C (84F) ambient, 36C (96F), 36C (96F), 36C (96F), 36C (96F), 36C (96F), 57C (134F), 37C (98F), 57C (134F), 44C (111F), 59C (138F), 40C (104F), 45C (113F), 58C (136F), 42C (107F)"
d = {
    f"TEST sensor num {index}": temp
    for index, temp in enumerate(re.findall(pattern, long_string))
}

print(json.dumps(d, indent=2))

Output: Output:

{
  "TEST sensor num 0": "29C",
  "TEST sensor num 1": "36C",
  "TEST sensor num 2": "36C",
  "TEST sensor num 3": "36C",
  "TEST sensor num 4": "36C",
  "TEST sensor num 5": "36C",
  "TEST sensor num 6": "57C",
  "TEST sensor num 7": "37C",
  "TEST sensor num 8": "57C",
  "TEST sensor num 9": "44C",
  "TEST sensor num 10": "59C",
  "TEST sensor num 11": "40C",
  "TEST sensor num 12": "45C",
  "TEST sensor num 13": "58C",
  "TEST sensor num 14": "42C"
}

this code will works:此代码将起作用:

from sys import stdin
inputString=stdin.readline().split()
outputDict=dict()
for i in range(len(inputString)//2):
    outputDict["test sensor num{}".format(i)] = inputString[2*i]

print(outputDict) 

if input be: 29C (84F), 36C (96F), 36C (96F)如果输入为:29C (84F), 36C (96F), 36C (96F)

output will be: {'test sensor num0': '29C', 'test sensor num1': '36C', 'test sensor num2': '36C'} output 将是:{'test sensor num0': '29C', 'test sensor num1': '36C', 'test sensor num2': '36C'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM