python逐行读取字符串_Python:从文件读取和替换字符串(带有特殊字符)时出错...

1586010002-jmsa.png

a.json file:

{

"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:

Open file in a read mode,

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:

Open file in read mode,

Since the file a.json has JSON data, get json file loaded, convert json object to JSON-string and then replace it.

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'}

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

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'}

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.

解决方案

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

Which, for your JSON, will yield:

{

"a": "b",

"key": "abcd nodes",

"c": "d"

}

(Extra new lines added by print).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值