循环并复制键值对

时间:2014-08-11 09:42:28

标签: python regex bash

比如说,我有两个包含以下内容的文本文件:

文件1

  

" key_one" ="键一号的字符串值"
  " key_two" ="键2的字符串值"
  //评论//
  " key_three" ="键2的字符串值"

文件2

  

//评论
  " key_one" =" key_one"
  //评论
  " key_two" =" key_two"

现在,我想循环遍历文件1 并获取每个键和字符串值(如果它不是注释行)。然后,我想搜索文件2 以获取密钥,如果找到密钥,请将其字符串值替换为文件1

中的字符串值

我猜想使用一些正则表达式会很好,但那是我的计划失败的地方。虽然我越来越好,但我对正则表达式并不是很了解。

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

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

我希望使用bash脚本或python脚本完成此任务。我尝试在python中使用正则表达式搜索和匹配函数,但收效甚微。

4 个答案:

答案 0 :(得分:1)

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

只需使用一些内置的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 :(得分:1)

您可以使用FILE1创建字典,然后使用它来替换{​​{1}}

中的值
FILE2

答案 2 :(得分:0)

import pprint

def get_values(f): file1 = open(f,"r").readlines() values = {} for line in file1: if line[:2] !="//" and "=" in line: #print line key, value = line.split("=") #print key, value values[key]=value

return values

def replace_values(v1, v2): for key in v1: v = v1[key] if key in v2: v2[key]=v

file1_values = get_values("file1.txt") file2_values = get_values("file2.txt")

print "BEFORE" print pprint.pprint(file1_values) print pprint.pprint(file2_values)

replace_values(file1_values, file2_values)

print "AFTER" print pprint.pprint(file1_values) print pprint.pprint(file2_values)

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

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


        BEFORE
    {'"key_one" ': ' "String value for key one"\n',
     '"key_three" ': ' "String value for key two"',
     '"key_two" ': ' "String value for key two"\n'}

    {'"key_one" ': ' "key_one"\n', '"key_two" ': ' "key_two"'}

    AFTER
    {'"key_one" ': ' "String value for key one"\n',
     '"key_three" ': ' "String value for key two"',
     '"key_two" ': ' "String value for key two"\n'}

    {'"key_one" ': ' "String value for key one"\n',
     '"key_two" ': ' "String value for key two"\n'}
    

答案 3 :(得分:-1)

使用这里给出的一些提示,我编写了自己的解决方案。它可能会在一些地方得到改善,但我很高兴自己创建解决方案而不仅仅是复制和粘贴别人的答案。所以,我的解决方案:

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()

如果可以的话,我会给出一些有用的答案。