python - 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
这工作正常并按预期替换字符串但是
当我尝试以下方法时不是
方法一:
以读取模式打开文件,
逐行获取并替换字符串
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" }
方法二:
以阅读模式打开文件,
由于文件 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'}
- 蟒蛇版本:2.7.15
- 使用来自 SO 答案之一的 byteify 代码。
- JSON 文件很大,无法进行手动搜索和替换。
- 在上面的示例中仍然尝试了 python 中的 ' 和 " 没有区别。
解决方案
虽然我当然不建议在 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
)。
推荐阅读
- python - 如果不在括号之间,则匹配字符
- xml - 如何创建从标准输入读取的 Scala XMLEventReader?
- scala - 使用 GroupBy 的灵活查询
- c - 在内存 C 中查找指针的值
- arrays - Delphi中的无效类型转换错误处理JsonResult
- python - python解释器是否简化了数学表达式?
- javascript - 条件为假时如何暂停
- python-3.x - 用于从 Google Drive 自动更新文件的 Python 脚本
- apache-spark - Spark合并不减少分区数
- sql-server - 在 SQL Server 中将行转为列,将列转为行