简体   繁体   English

Python:从文件中读取和替换字符串(带有特殊字符)时出错

[英]Python: Error while reading and replacing String(with special characters) from file

a.json file: a.json 文件:

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

following code I tried:以下代码我试过:

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)

output:输出:

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

This is working fine and replacing string as expected but这工作正常并按预期替换字符串但是

it is not when I tried the following approaches当我尝试以下方法时不是

Approach 1:方法一:

  1. Open file in a read mode,以读取模式打开文件,

  2. get line by line and replace string逐行获取并替换字符串

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

output:输出:

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

Approach 2:方法二:

  1. Open file in reading mode,以阅读模式打开文件,

  2. Since the file a.json has JSON data, get JSON file loaded, convert JSON object to JSON-string and then replace it.由于文件a.json 有JSON 数据,加载JSON 文件,将JSON 对象转换为JSON-string,然后替换它。

Code:代码:

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

output:输出:

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

Approach 3:方法三:

I assume Unicode character in JSON string might be creating a problem so converted Unicode string to normal string and then tried to replace string我假设 JSON 字符串中的 Unicode 字符可能会产生问题,因此将 Unicode 字符串转换为普通字符串,然后尝试替换字符串

code:代码:

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)

output:输出:

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

  • python version:2.7.15蟒蛇版本:2.7.15
  • using byteify code from one of the SO answer.使用来自 SO 答案之一的 byteify 代码。
  • JSON file is big and cannot do the manual search and replace. JSON 文件很大,无法进行手动搜索和替换。
  • There is no difference in ' and " in python still tried in above example.在上面的例子中仍然尝试过的python中的 ' 和 " 没有区别。

While I certainly do not recommend any sort of context-unaware search & replace in a hierarchical structure like JSON, your main issue is that the string you're searching for in your JSON file has escaped quotations (literal \\ characters) so you have to account for those as well if you want to do plain text search.虽然我当然不建议在 JSON 等层次结构中进行任何类型的上下文无关搜索和替换,但您的主要问题是您在 JSON 文件中搜索的字符串已转义引号(文字\\字符),因此您必须如果您想进行纯文本搜索,也可以考虑这些。 You can use either raw strings or add the backslashes yourself, something like:您可以使用原始字符串或自己添加反斜杠,例如:

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

Which, for your JSON, will yield:其中,对于您的 JSON,将产生:

{

  "a": "b",

  "key": "abcd nodes",

  "c": "d"

}

(Extra new lines added by print ). (由print添加的额外新行)。

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

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