Python:从文件读取和替换字符串(带有特殊字符)时出错 [英] Python: Error while reading and replacing String(with special characters) from file
问题描述
a.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)
输出:
颜色" =黑色"和"api" ="demo-application-v1"
"color" = 'black' AND "api" = 'demo-application-v1'
颜色" =黑色"和"api" ="demo-application-v1"
"color" = 'black' AND "api" = 'demo-application-v1'
图:abcd节点
图:abcd节点
这可以正常工作,并且可以按预期替换字符串,但是
This is working fine and replacing string as expected but
不是我尝试以下方法时
方法1:
-
以读取模式打开文件,
Open file in a read mode,
逐行获取并替换字符串
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 \"='黑色'AND \" api \"= "demo-application-v1"节点",
"key": "graph: \"color\" = 'black' AND \"api\" = 'demo-application-v1' node",
"c":"d"
}
方法2:
-
以读取模式打开文件
Open file in read mode,
由于文件a.json具有JSON数据,请加载json文件,然后将json对象转换为JSON字符串,然后替换它.
Since the file a.json has JSON data, get json file loaded, convert json object to JSON-string and then replace it.
代码:
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 \'节点'}
z: {u'a': u'b', u'c': u'd', u'key': u'graph: "color" = \'black\' AND "api" = \'demo-application-v1\' node'}
方法3:
我认为JSON字符串中的Unicode字符可能会造成问题,因此将Unicode字符串转换为普通字符串,然后尝试替换字符串
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
代码:
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 \'节点'}
a: {'a': 'b', 'c': 'd', 'key': 'graph: "color" = \'black\' AND "api" = \'demo-application-v1\' node'}
- python版本:2.7.15
- 使用其中一个SO答案中的字节码.
- JSON文件很大,无法进行手动搜索和替换.
- 在上面的示例中仍尝试在python中的'和'没有区别.
- python version:2.7.15
- using byteify code from one of the SO answer.
- JSON file is big and cannot do the manual search and replace.
- There is no difference in ' and " in python still tried in above example.
推荐答案
虽然我当然不建议使用任何类型的不了解上下文的搜索&替换为JSON之类的层次结构,您的主要问题是,您在JSON文件中搜索的字符串已转义了引号(文字\
字符),因此如果您要进行纯文本处理,则也必须考虑这些问题搜索.您可以使用原始字符串或添加反斜杠表示自己,例如:
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. 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))
对于您的JSON,这将产生:
Which, for your JSON, will yield:
{
"a": "b",
"key": "abcd nodes",
"c": "d"
}
(print
添加的其他新行).
这篇关于Python:从文件读取和替换字符串(带有特殊字符)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!