首页 > 解决方案 > Python:从文件中读取和替换字符串(带有特殊字符)时出错

问题描述

一个.json文件:

{
  "a": "b",
  "key": "graph: \"color\" = 'black' AND \"api\" = 'demo-application-v1' nodes",
  "c": "d"
}

我尝试了以下代码:

string_to_be_replace = "abcd"
string_to = "graph: \"color\" = 'black' AND \"api\" = 'demo-application-v1' nodes"
string_to_be_identified = "\"color\" = \'black\' AND \"api\" = \'demo-application-v1\'"
string_to_be_identified1 = '"color" = \'black\' AND "api" = \'demo-application-v1\''

print string_to_be_identified
print string_to_be_identified1
print string_to.replace(string_to_be_identified1,string_to_be_replace)
print string.replace(string_to, string_to_be_identified,string_to_be_replace)

输出:

"color" = 'black' AND "api" = 'demo-application-v1'
"color" = 'black' AND "api" = 'demo-application-v1'
graph: abcd nodes
graph: abcd nodes

这工作正常并按预期替换字符串但是

当我尝试以下方法时不是

方法一:

  1. 以读取模式打开文件,

  2. 逐行获取并替换字符串

with open(path + '/a.json', 'r') as file:
    read_lines = file.readlines()
    for line in read_lines:
        print line.replace(string_to_be_identified,string_to_be_replace)
file.close()

输出:

{
  "a": "b",
  "key": "graph: \"color\" = 'black' AND \"api\" ='demo-application-v1' node",
  "c": "d"
}

方法二:

  1. 以阅读模式打开文件,

  2. 由于文件 a.json 有 JSON 数据,因此加载 JSON 文件,将 JSON 对象转换为 JSON 字符串,然后替换它。

代码:

 with open(path + '/a.json', 'r') as file:
    loadedJson = json.load(file)
    print "z: " + str(loadedJson).replace(string_to_be_identified, string_to_be_replace)
file.close()

输出:

z: {u'a': u'b', u'c': u'd', u'key': u'graph: "color" = 'black' AND "api" = 'demo-application-v1 '节点'}

方法3:

我假设 JSON 字符串中的 Unicode 字符可能会产生问题,因此将 Unicode 字符串转换为普通字符串,然后尝试替换字符串

代码:

def byteify(input):
    if isinstance(input, dict):
        return {byteify(key): byteify(value)
                for key, value in input.iteritems()}
    elif isinstance(input, list):
        return [byteify(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode('utf-8')
    else:
        return input

with open(path + '/a.json', 'r') as file:
    loadedJson = json.load(file)
    js = byteify(loadedJson)
    print "a: " + str(js).replace(string_to_be_identified, string_to_be_replace)

输出:

a: {'a': 'b', 'c': 'd', 'key': 'graph: "color" = 'black' AND "api" = 'demo-application-v1' node'}

标签: pythonjsonpython-2.7replace

解决方案


虽然我当然不建议在 JSON 等层次结构中进行任何类型的上下文不感知搜索和替换,但您的主要问题是您在 JSON 文件中搜索的字符串已转义引号(文字\字符),因此您必须考虑如果您想进行纯文本搜索,也适用于那些。您可以使用原始字符串或自己添加反斜杠,例如:

str_search = r"graph: \"color\" = 'black' AND \"api\" = 'demo-application-v1'"
# or, if you prefer to manually write down the string instead of declaring it 'raw':
# str_search = "graph: \\\"color\\\" = 'black' AND \\\"api\\\" = 'demo-application-v1'"
str_replace = "abcd"

with open("/path/to/your.json", "r") as f:
    for line in f:
        print(line.replace(str_search, str_replace))

对于您的 JSON,这将产生:

{

  “a”:“b”,

  "key": "abcd 节点",

  “c”:“d”

}

(由添加的额外新行print)。


推荐阅读