Remove quotation markers between quotation marks












4















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









share|improve this question

























  • stackoverflow.com/questions/26745519/…

    – Hugo Luis Villalobos Canto
    Jan 18 at 20:29
















4















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









share|improve this question

























  • stackoverflow.com/questions/26745519/…

    – Hugo Luis Villalobos Canto
    Jan 18 at 20:29














4












4








4


1






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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















6














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





share|improve this answer


























  • 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











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


}
});














draft saved

draft discarded


















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









6














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





share|improve this answer


























  • 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
















6














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





share|improve this answer


























  • 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














6












6








6







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage