Python 循环并复制键值对

Python 循环并复制键值对,python,regex,bash,Python,Regex,Bash,例如,我有两个文本文件,其中包含以下内容: For each line of File 1 Check if it's a comment line by checking that it starts with '//' If not a comment line, split it to `key` and `value` Store the key/value in a dictionary For each line of File 2 Check if

例如,我有两个文本文件,其中包含以下内容:

For each line of File 1
    Check if it's a comment line by checking that it starts with '//'
    If not a comment line, split it to `key` and `value`
    Store the key/value in a dictionary

For each line of File 2
    Check if it's a comment line by checking that it starts with '//'
    If not a comment line, split it to `key` and `value`
    Check the dictionary to see if the key exists
    Output to the file as necessary
文件1

“key_one”=“key one的字符串值”
“键二”=“键二的字符串值”
//评论//
“键三”=“键二的字符串值”

文件2

//评论
“钥匙一号”=“钥匙一号”
//评论
“按键二”=“按键二”

现在,我想遍历文件1,找出每个键和字符串值(如果不是注释行)。然后我想搜索文件2中的键,如果找到了,则用文件1中的字符串值替换其字符串值

我想在这里使用一些正则表达式会很好,但这就是我的计划失败的地方。我对正则表达式的理解不是很透彻,尽管我正在进步

下面是我用来匹配键的正则表达式:
“^\”\w*\”
下面是我试图匹配字符串的正则表达式:
“=[\”a-zA-Z0-9]*”

这些可能不是正确的或最好的,所以请随时纠正我


我希望使用bash脚本或python脚本来完成这项任务。我曾尝试在python中使用regex搜索和匹配函数,但收效甚微。

我从某处听到一句话:“如果您有问题,并试图用正则表达式来解决,那么您现在有两个问题。”

只需使用一些内置的Python字符串方法,例如
startswith()
split()
,就可以轻松实现您想要实现的目标,而无需使用任何正则表达式

简而言之,您可以执行以下操作:

For each line of File 1
    Check if it's a comment line by checking that it starts with '//'
    If not a comment line, split it to `key` and `value`
    Store the key/value in a dictionary

For each line of File 2
    Check if it's a comment line by checking that it starts with '//'
    If not a comment line, split it to `key` and `value`
    Check the dictionary to see if the key exists
    Output to the file as necessary

您可以从
文件1
创建字典,然后使用它替换
文件2

import fileinput
import re

pattern = re.compile(r'"(.*?)"\s+=\s+"(.*?)"')

with open('FILE1', 'r') as f:
    values = dict(pattern.findall(f.read()))

for line in fileinput.input('FILE2', inplace=True):
    match = pattern.match(line)
    if match:
        line = '"%s" = "%s"' % (match.group(1), values[match.group(1)])
    print line.strip()
def替换_值(v1、v2): 对于输入v1: v=v1[键] 如果输入v2: v2[键]=v

file1_values=获取_值(“file1.txt”) file2_values=获取_值(“file2.txt”)

打印“之前” 打印pprint.pprint(文件1\u值) 打印pprint.pprint(文件2_值)

替换_值(文件1_值、文件2_值)

打印“之后” 打印pprint.pprint(文件1\u值) 打印pprint.pprint(文件2_值)

如果文本文件是可预测的,那么您可以使用类似的东西

上述代码将执行您想要的操作,并用以下输出替换这些值:

import fileinput

translations = {}

with open('file1.txt', 'r') as fileOne:
    trans = fileOne.readlines()

    for line in trans:
        if (line.startswith("\"")):
            key, value = line.strip().split(" = ")
            translations[key] = value

for line in fileinput.input('file2.txt', inplace=True):
    if (line.startswith("\"")):
        key, value = line.strip().split(" = ")
        if key in translations:
            line = "{} = {}".format(key, translations[key])
    print line.strip()
之前 {''key_one':''key one的字符串值'\n', ““键三”:““键二的字符串值”, ““键2”:““键2的字符串值”\n'} {'key_one':'key_one'\n','key_one':'key_one'} 之后 {''key_one':''key one的字符串值'\n', ““键三”:““键二的字符串值”, ““键2”:““键2的字符串值”\n'} {''key_one':''key one的字符串值'\n', ““键2”:““键2的字符串值”\n'}
使用这里给出的一些技巧,我编写了自己的解决方案。它可能在一些地方得到改进,但我很高兴自己创建了解决方案,而不只是复制和粘贴其他人的答案。因此,我的解决方案:


如果可以的话,我仍然会对有用的答案投一些赞成票。

如果文件是这样的话,你不需要正则表达式。你的问题是什么?你尝试过什么吗?对于python:为什么不遍历文件1并创建一个字典,然后遍历文件2并用字典中的键替换可以找到的值解析从文件1到dict的所有键/值对的方法是:dict(re.findall(r“^\”(.*?“\s=\s\”(.*?)、file1.read()、re.MULTILINE))我喜欢这句话!我通常会尽量远离regex,不知道为什么我没有想到这个“更简单的”“以前的解决方案。我会的,但两天内我不会接受我自己的答案。为什么会投反对票?如果能提供相关评论,我会很有帮助,这样我就知道我可以改进的地方/内容。
import fileinput

translations = {}

with open('file1.txt', 'r') as fileOne:
    trans = fileOne.readlines()

    for line in trans:
        if (line.startswith("\"")):
            key, value = line.strip().split(" = ")
            translations[key] = value

for line in fileinput.input('file2.txt', inplace=True):
    if (line.startswith("\"")):
        key, value = line.strip().split(" = ")
        if key in translations:
            line = "{} = {}".format(key, translations[key])
    print line.strip()