在dict、Python中添加for循环的结果

2024-05-23 21:05:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含很多信息的txt文件。在我的脚本中,我打开这个文件并使用内置函数和regex删除不必要的内容,这是在for循环中完成的。你知道吗

假设txt文件如下所示:

猴子走开

大象在上

等等。。。你知道吗


我的剧本:

x = 'XXX'
y = 'YYY'
value = ''
name = ''
func_file, functionality_file_context = open_func_file(args)
for fline in functionality_file_context:
    if fline.strip():  # ignore empty lines or lines with only whitespace
        commentregex = re.compile('^[^#]')  # ignore lines that start with comment (#)
        if (commentregex.match(fline)):
            # replace tabs with whitespace
            replacetab = re.compile(r'\s+')
            fline = replacetab.sub(' ', fline)

            function_line = fline.split(' ', 2)
            if len(function_line) != 3:
                exit("Something wrong with this line: " + fline.strip() + "\" in: " + func_file)

            name = x + function_line[0].strip() + y
            value = func_override_parameter_value(fline, func_file, function_line,value)
            print name + value


myDict = {
"Bike": 0,
"Car": 2,
name: value
}

another_function(args.path, myDict)

func_override_parameter_value检查应该给出什么值,例如OFF-OFF匹配等。例如OFF-OFF=1,ON-ON=2

通过打印(print name + value),我得到了所有具有正确赋值的名称。你知道吗

打开函数文件函数:

def open_func_file(args):
    functionality_file_context = ''
    func_file = ''
    for func_file in args.functionality_file:
        functionality_file_context = open(func_file).readlines()
    return func_file, functionality_file_context

现在说到重点:

我在for循环运行之后有一个dict,它已经包含一些键和值,例如:

myDict = {
"Bike": 0,
"Car": 2,
}

我想从我刚刚“解析”的文件中添加新的键和值。我试着在dict中调用name和value:

myDict = {
"Bike": 0,
"Car": 2,
name: value
}

最终结果应该是:

myDict = {
"Bike": 0,
"Car": 2,
"XXXMonkeyYYY": 1,
"XXXElephantYYY": 2
}

但这只是给我“大象”:2和跳过猴子。你知道怎么解决这个问题吗?顺序无关紧要。你知道吗

进行了编辑


Tags: 文件nameforvaluewithcontextlineargs
2条回答

将添加到字典的代码放在循环中的print旁边。namevalue一次只能保存一个值,它们无法神奇地记住它们曾经经历过的一切,也无法知道插入dict的正确方法

如果@alex hall的直截了当的猜测和简洁的建议对OP来说不够明确,可能是这样的:

#! /usr/bin/env python
from __future__ import print_function
import re


def open_func_file(args):
    functionality_file_context = ''
    func_file = ''
    for func_file in args.functionality_file:
        functionality_file_context = open(func_file).readlines()
    return func_file, functionality_file_context


def func_override_parameter_value(
        fline, func_file, function_line, value):
    """Mocking the undefined function."""
    return 42

args = "some_file_name"
x = 'XXX'
y = 'YYY'
value = ''
name = ''
myDict = {
    "Bike": 0,
    "Car": 2,
}
commentregex = re.compile('^[^#]')  # ignore lines that start with comment (#)
func_file, functionality_file_context = open_func_file(args)
for fline in functionality_file_context:
    if fline.strip():  # ignore empty lines or lines with only whitespace
        if (commentregex.match(fline)):
            # replace tabs with whitespace
            replacetab = re.compile(r'\s+')
            fline = replacetab.sub(' ', fline)

            function_line = fline.split(' ', 2)
            if len(function_line) != 3:
                exit("Something wrong with this line: "
                     "" + fline.strip() + "\" in: " + func_file)

            name = x + function_line[0].strip() + y
            value = func_override_parameter_value(
                fline, func_file, function_line, value)
            print(name + str(value))
            myDict[name] = value

提示:向下滚动到上面代码的最后一行,其中只说明:

myDict[name] = value

相关问题 更多 >