سؤال

>>> import simplejson
>>> data={'s': 1, 'd': {'kwds': {u'"ramana"': {u'"ramana"': [(0L, 7L)]}}}}
>>> print simplejson.dumps(data, ensure_ascii=False)

Then I got like this.

{"s": 1, "d": {"kwds": {"\"ramana\"": {"\"ramana\"": [[0, 7]]}}}}

But I want to get like:

{"s": 1, "d": {"kwds": {""ramana"": {""ramana"": [[0, 7]]}}}}

How to do that?

هل كانت مفيدة؟

المحلول

It is just a representation of data. You can ignore it.

Why it is represented like that?

"" means an empty string in Python. So, if you have sentence like this

Welcome to "SO" Guys

When the computer processes it, it might become

"Welcome to "SO" Guys"

And it will be treated as two different strings ("Welcome to " and " Guys"). Now, What is SO, here? That's why simplejson escapes the " character with \. But the data is safe,

Check this example

data = 'Welcome to "SO" Guys'
import simplejson
print simplejson.dumps(data)
print data

Output

"Welcome to \"SO\" Guys"
Welcome to "SO" Guys

And simplejson does that because of JSON's specification

2.5. Strings

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with
quotation marks. All Unicode characters may be placed within the
quotation marks except for the characters that must be escaped:
quotation mark, reverse solidus, and the control characters (U+0000
through U+001F).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top