1

I want to replace a string with (double quotes + string). Need to use it into python.

Input : {responseHeader:{status:0,QTime:94}}

Output : {"responseHeader":{"status":0,"QTime":94}}

Tried /[^\d\W]+/g regex to get only string but don't know how to replace it with (double quotes + string).

2
  • Your input data doesn't have single quotes. Is it a JSON data? Commented Mar 27, 2015 at 11:08
  • My input doesn't have single quotes. Input data which I am getting as from different source that already not in JSON. That's why I need to convert it into JSON. Commented Mar 27, 2015 at 16:20

2 Answers 2

2

Try this

>>> import re
>>> inp = '{responseHeader:{status:0,QTime:94}}'
>>> re.sub(r'([a-zA-Z]+)',r'"\1"',inp)
'{"responseHeader":{"status":0,"QTime":94}}'
Sign up to request clarification or add additional context in comments.

1 Comment

@vks Yeah! Damn lucky today! I woke up looking at the mirror
2
([a-zA-Z]+)

Try this.Replace by "\1".See demo.

https://regex101.com/r/sJ9gM7/18#python

import re
p = re.compile(r'([a-zA-Z]+)', re.MULTILINE)
test_str = "{responseHeader:{status:0,QTime:94}}"
subst = "\"\1\""

result = re.sub(p, subst, test_str)

1 Comment

@BhargavRao yeah after so many warnings from u ,now i keep that in mind :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.