我的字典如下所示:
'{\n "bindings": [\n {\n "members": [\n "user:[email protected]"\n ],\n "role": "roles/browser"\n },\n {\n "members": [\n "serviceAccount:[email protected]",\n "user:[email protected]"\n ],\n "role": "roles/owner"\n },\n {\n "members": [\n "user:[email protected]"\n ],\n "role": "roles/viewer"\n }\n ],\n "etag": "BwVvsH-HhI0=",\n "version": 1\n}\n'
我需要从中删除\n
;但是strip()
没有在做。
我们还有其他方法可以从顶部移除它吗?!?
问候,Rohith
鉴于那里有一个JSON字符串,您应该使用以下json
模块:
import json
my_dict = json.loads('{\n "bindings": [\n {\n "members": [\n "user:[email protected]"\n ],\n "role": "roles/browser"\n },\n {\n "members": [\n "serviceAccount:[email protected]",\n "user:[email protected]"\n ],\n "role": "roles/owner"\n },\n {\n "members": [\n "user:[email protected]"\n ],\n "role": "roles/viewer"\n }\n ],\n "etag": "BwVvsH-HhI0=",\n "version": 1\n}\n')
那么您就可以将其作为一本实际的词典,并可以根据需要进行处理。如果要打印时不带换行符,则可以:
print(my_dict)
或者您可以将其用作dict,具体取决于您实际尝试删除换行符的目的。
如果您确实有该字符串,并且只想摆脱其中的换行符,则可以使用.replace
:
'{\n "bindings": [\n {\n "members": [\n "user:[email protected]"\n ],\n "role": "roles/browser"\n },\n {\n "members": [\n "serviceAccount:[email protected]",\n "user:[email protected]"\n ],\n "role": "roles/owner"\n },\n {\n "members": [\n "user:[email protected]"\n ],\n "role": "roles/viewer"\n }\n ],\n "etag": "BwVvsH-HhI0=",\n "version": 1\n}\n'.replace('\n', '')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句