Changing ' to " in printed representation of dictionaries
Terry Reedy
tjreedy at udel.edu
Thu Dec 2 18:37:00 EST 2010
On 12/2/2010 6:06 PM, Burton Samograd wrote:
> Hello,
>
> I was wondering if there was a way to change the quote character for
> keys in string representation of dictionaries, so that they will be JSON
> equivalent. For example:
>
>>>> x = { 'x': 1, 'y': 2 }
> { 'x': 1, 'y': 2 }
>>>> `x`
> "{ 'x': 1, 'y': 2 }" # close but not quite a JSON string
>>>> `x`.replace("'", '"')
> '{ "x": 1, "y": 2 }' # JSON and python compatible
>
> So the question is, is there an automatic way to tell python to use "
> instead of ' when doing a repr of lists?
You meant dicts rather than lists, but the issue is repr of strings.
And no, there is no control of that (without subclassing strings).
Note that dicts are not limited to string keys, or even keys
prepresentable in json. If you want the json representation, when you
know it is possible, use the json module!
--
Terry Jan Reedy
More information about the Python-list
mailing list