Remove quotation markers between quotation marks
I have a string in json formation like that
{"1":"abc"abc"abc","2":"xyz"xyz"xyz"}
But if I want to tranform it into json data, I need to remove '"' between '"' and get a string like below
{"1":"abcabcabc","2":"xyzxyzxyz"}
I tried using re.sub to do that, but failed. Anyone could help me with that?
My script is below:
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
b = r.sub('', a)
print(b)
When I ran the script, the outcome is below:
Traceback (most recent call last):
File "./_t1.py", line 5, in <module>
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 233, in compile
return _compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 566, in compile
code = _code(p, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 551, in _code
_compile(code, p.data, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 187, in _compile
_compile(code, av, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 160, in _compile
raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern
python
add a comment |
I have a string in json formation like that
{"1":"abc"abc"abc","2":"xyz"xyz"xyz"}
But if I want to tranform it into json data, I need to remove '"' between '"' and get a string like below
{"1":"abcabcabc","2":"xyzxyzxyz"}
I tried using re.sub to do that, but failed. Anyone could help me with that?
My script is below:
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
b = r.sub('', a)
print(b)
When I ran the script, the outcome is below:
Traceback (most recent call last):
File "./_t1.py", line 5, in <module>
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 233, in compile
return _compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 566, in compile
code = _code(p, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 551, in _code
_compile(code, p.data, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 187, in _compile
_compile(code, av, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 160, in _compile
raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern
python
stackoverflow.com/questions/26745519/…
– Hugo Luis Villalobos Canto
Jan 18 at 20:29
add a comment |
I have a string in json formation like that
{"1":"abc"abc"abc","2":"xyz"xyz"xyz"}
But if I want to tranform it into json data, I need to remove '"' between '"' and get a string like below
{"1":"abcabcabc","2":"xyzxyzxyz"}
I tried using re.sub to do that, but failed. Anyone could help me with that?
My script is below:
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
b = r.sub('', a)
print(b)
When I ran the script, the outcome is below:
Traceback (most recent call last):
File "./_t1.py", line 5, in <module>
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 233, in compile
return _compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 566, in compile
code = _code(p, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 551, in _code
_compile(code, p.data, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 187, in _compile
_compile(code, av, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 160, in _compile
raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern
python
I have a string in json formation like that
{"1":"abc"abc"abc","2":"xyz"xyz"xyz"}
But if I want to tranform it into json data, I need to remove '"' between '"' and get a string like below
{"1":"abcabcabc","2":"xyzxyzxyz"}
I tried using re.sub to do that, but failed. Anyone could help me with that?
My script is below:
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
b = r.sub('', a)
print(b)
When I ran the script, the outcome is below:
Traceback (most recent call last):
File "./_t1.py", line 5, in <module>
r = re.compile(r'(?<!:)(?<=.+)"|(?<!,)"|"(?!}|,)')
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 233, in compile
return _compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 566, in compile
code = _code(p, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 551, in _code
_compile(code, p.data, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 187, in _compile
_compile(code, av, flags)
File "/home/emc/ssd/anaconda3/lib/python3.6/sre_compile.py", line 160, in _compile
raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern
python
python
edited Jan 18 at 20:44
Jean-François Fabre
102k954111
102k954111
asked Jan 18 at 20:23
JianhaoJianhao
264
264
stackoverflow.com/questions/26745519/…
– Hugo Luis Villalobos Canto
Jan 18 at 20:29
add a comment |
stackoverflow.com/questions/26745519/…
– Hugo Luis Villalobos Canto
Jan 18 at 20:29
stackoverflow.com/questions/26745519/…
– Hugo Luis Villalobos Canto
Jan 18 at 20:29
stackoverflow.com/questions/26745519/…
– Hugo Luis Villalobos Canto
Jan 18 at 20:29
add a comment |
1 Answer
1
active
oldest
votes
That works if your data doesn't contain ,
or :
because we need some anchors to untangle this mess:
import re
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
b = re.sub('"((?:[^,:]|")*)"',lambda m : '"{}"'.format(m.group(1).replace('"','')),a)
>>> b
'{"1":"abcdefg","2":"xyzxyzxyz"}'
- regex matches the string between quotes and replacement function removes the inner quotes.
- we create an inner non-capturing
(?:[^,:]|")
group to tell to match quotes or anything but comma and colon.
now b
can be parsed as json:
>>> import json
>>> json.loads(b)
{'1': 'abcdefg', '2': 'xyzxyzxyz'}
now what if the string contains :
? the solution above doesn't work. We have to adapt it:
- split according to
":"
(with possible spaces) - apply a similar regex as above (with just the first quote removed) on all elements of the split list
- join back the elements with
":"
like this:
import re,json
# a lot of colons in keys & values
a='{"1":"a:bc"de"fg","2:":"xy::z"xyz"xyz"}'
b = '":"'.join(re.sub('((?:[^,:]|")*)"',lambda m : '{}"'.format(m.group(1).replace('"','')),x) for x in re.split('"s*:s*"',a))
print(json.loads(b))
Results in proper parsing of json
:
{'1': 'a:bcdefg', '2:': 'xy::zxyzxyz'}
Thank you. Moreover, what if there is any ':' in the values?
– Jianhao
Jan 20 at 13:13
that will lose my regular expression.
– Jean-François Fabre
Jan 20 at 14:13
If there is any ':' in the values. What regular expression should be used?
– Jianhao
Jan 20 at 20:19
edited for a more complex solution, which works
– Jean-François Fabre
Jan 20 at 20:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54260946%2fremove-quotation-markers-between-quotation-marks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
That works if your data doesn't contain ,
or :
because we need some anchors to untangle this mess:
import re
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
b = re.sub('"((?:[^,:]|")*)"',lambda m : '"{}"'.format(m.group(1).replace('"','')),a)
>>> b
'{"1":"abcdefg","2":"xyzxyzxyz"}'
- regex matches the string between quotes and replacement function removes the inner quotes.
- we create an inner non-capturing
(?:[^,:]|")
group to tell to match quotes or anything but comma and colon.
now b
can be parsed as json:
>>> import json
>>> json.loads(b)
{'1': 'abcdefg', '2': 'xyzxyzxyz'}
now what if the string contains :
? the solution above doesn't work. We have to adapt it:
- split according to
":"
(with possible spaces) - apply a similar regex as above (with just the first quote removed) on all elements of the split list
- join back the elements with
":"
like this:
import re,json
# a lot of colons in keys & values
a='{"1":"a:bc"de"fg","2:":"xy::z"xyz"xyz"}'
b = '":"'.join(re.sub('((?:[^,:]|")*)"',lambda m : '{}"'.format(m.group(1).replace('"','')),x) for x in re.split('"s*:s*"',a))
print(json.loads(b))
Results in proper parsing of json
:
{'1': 'a:bcdefg', '2:': 'xy::zxyzxyz'}
Thank you. Moreover, what if there is any ':' in the values?
– Jianhao
Jan 20 at 13:13
that will lose my regular expression.
– Jean-François Fabre
Jan 20 at 14:13
If there is any ':' in the values. What regular expression should be used?
– Jianhao
Jan 20 at 20:19
edited for a more complex solution, which works
– Jean-François Fabre
Jan 20 at 20:29
add a comment |
That works if your data doesn't contain ,
or :
because we need some anchors to untangle this mess:
import re
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
b = re.sub('"((?:[^,:]|")*)"',lambda m : '"{}"'.format(m.group(1).replace('"','')),a)
>>> b
'{"1":"abcdefg","2":"xyzxyzxyz"}'
- regex matches the string between quotes and replacement function removes the inner quotes.
- we create an inner non-capturing
(?:[^,:]|")
group to tell to match quotes or anything but comma and colon.
now b
can be parsed as json:
>>> import json
>>> json.loads(b)
{'1': 'abcdefg', '2': 'xyzxyzxyz'}
now what if the string contains :
? the solution above doesn't work. We have to adapt it:
- split according to
":"
(with possible spaces) - apply a similar regex as above (with just the first quote removed) on all elements of the split list
- join back the elements with
":"
like this:
import re,json
# a lot of colons in keys & values
a='{"1":"a:bc"de"fg","2:":"xy::z"xyz"xyz"}'
b = '":"'.join(re.sub('((?:[^,:]|")*)"',lambda m : '{}"'.format(m.group(1).replace('"','')),x) for x in re.split('"s*:s*"',a))
print(json.loads(b))
Results in proper parsing of json
:
{'1': 'a:bcdefg', '2:': 'xy::zxyzxyz'}
Thank you. Moreover, what if there is any ':' in the values?
– Jianhao
Jan 20 at 13:13
that will lose my regular expression.
– Jean-François Fabre
Jan 20 at 14:13
If there is any ':' in the values. What regular expression should be used?
– Jianhao
Jan 20 at 20:19
edited for a more complex solution, which works
– Jean-François Fabre
Jan 20 at 20:29
add a comment |
That works if your data doesn't contain ,
or :
because we need some anchors to untangle this mess:
import re
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
b = re.sub('"((?:[^,:]|")*)"',lambda m : '"{}"'.format(m.group(1).replace('"','')),a)
>>> b
'{"1":"abcdefg","2":"xyzxyzxyz"}'
- regex matches the string between quotes and replacement function removes the inner quotes.
- we create an inner non-capturing
(?:[^,:]|")
group to tell to match quotes or anything but comma and colon.
now b
can be parsed as json:
>>> import json
>>> json.loads(b)
{'1': 'abcdefg', '2': 'xyzxyzxyz'}
now what if the string contains :
? the solution above doesn't work. We have to adapt it:
- split according to
":"
(with possible spaces) - apply a similar regex as above (with just the first quote removed) on all elements of the split list
- join back the elements with
":"
like this:
import re,json
# a lot of colons in keys & values
a='{"1":"a:bc"de"fg","2:":"xy::z"xyz"xyz"}'
b = '":"'.join(re.sub('((?:[^,:]|")*)"',lambda m : '{}"'.format(m.group(1).replace('"','')),x) for x in re.split('"s*:s*"',a))
print(json.loads(b))
Results in proper parsing of json
:
{'1': 'a:bcdefg', '2:': 'xy::zxyzxyz'}
That works if your data doesn't contain ,
or :
because we need some anchors to untangle this mess:
import re
a='{"1":"abc"de"fg","2":"xyz"xyz"xyz"}'
b = re.sub('"((?:[^,:]|")*)"',lambda m : '"{}"'.format(m.group(1).replace('"','')),a)
>>> b
'{"1":"abcdefg","2":"xyzxyzxyz"}'
- regex matches the string between quotes and replacement function removes the inner quotes.
- we create an inner non-capturing
(?:[^,:]|")
group to tell to match quotes or anything but comma and colon.
now b
can be parsed as json:
>>> import json
>>> json.loads(b)
{'1': 'abcdefg', '2': 'xyzxyzxyz'}
now what if the string contains :
? the solution above doesn't work. We have to adapt it:
- split according to
":"
(with possible spaces) - apply a similar regex as above (with just the first quote removed) on all elements of the split list
- join back the elements with
":"
like this:
import re,json
# a lot of colons in keys & values
a='{"1":"a:bc"de"fg","2:":"xy::z"xyz"xyz"}'
b = '":"'.join(re.sub('((?:[^,:]|")*)"',lambda m : '{}"'.format(m.group(1).replace('"','')),x) for x in re.split('"s*:s*"',a))
print(json.loads(b))
Results in proper parsing of json
:
{'1': 'a:bcdefg', '2:': 'xy::zxyzxyz'}
edited Jan 20 at 20:29
answered Jan 18 at 20:32
Jean-François FabreJean-François Fabre
102k954111
102k954111
Thank you. Moreover, what if there is any ':' in the values?
– Jianhao
Jan 20 at 13:13
that will lose my regular expression.
– Jean-François Fabre
Jan 20 at 14:13
If there is any ':' in the values. What regular expression should be used?
– Jianhao
Jan 20 at 20:19
edited for a more complex solution, which works
– Jean-François Fabre
Jan 20 at 20:29
add a comment |
Thank you. Moreover, what if there is any ':' in the values?
– Jianhao
Jan 20 at 13:13
that will lose my regular expression.
– Jean-François Fabre
Jan 20 at 14:13
If there is any ':' in the values. What regular expression should be used?
– Jianhao
Jan 20 at 20:19
edited for a more complex solution, which works
– Jean-François Fabre
Jan 20 at 20:29
Thank you. Moreover, what if there is any ':' in the values?
– Jianhao
Jan 20 at 13:13
Thank you. Moreover, what if there is any ':' in the values?
– Jianhao
Jan 20 at 13:13
that will lose my regular expression.
– Jean-François Fabre
Jan 20 at 14:13
that will lose my regular expression.
– Jean-François Fabre
Jan 20 at 14:13
If there is any ':' in the values. What regular expression should be used?
– Jianhao
Jan 20 at 20:19
If there is any ':' in the values. What regular expression should be used?
– Jianhao
Jan 20 at 20:19
edited for a more complex solution, which works
– Jean-François Fabre
Jan 20 at 20:29
edited for a more complex solution, which works
– Jean-François Fabre
Jan 20 at 20:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54260946%2fremove-quotation-markers-between-quotation-marks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/questions/26745519/…
– Hugo Luis Villalobos Canto
Jan 18 at 20:29