使用Python生成一个C字符串的JSON文字 [英] using Python to generate a C string literal of JSON

查看:234
本文介绍了使用Python生成一个C字符串的JSON文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python中的字典,我想在JSON中序列化并转换为一个正确的C字符串,以便它包含一个对应于我的输入字典的有效的JSON字符串。我正在使用结果自动生成C源文件中的一行。得到它了?以下是一个例子:

 >>> import json 
>>> mydict = {'a':1,'b':'一个带引号和\t和\\backslashes的字符串}
>>> json.dumps(mydict)
'{a:1,b:带有\\quotes \\的字符串,\\t和\\\ \\'backslashes}'
>>> print(json.dumps(mydict))
{a:1,b:具有\quotes \和\t和\\backslashes的字符串}

我需要生成的是以下C字符串:

 {\a\:1,\b\:\带\\\\的引号\\\ \\和\\t和\\\\\backslashes\}



<换句话说,我需要在调用json.dumps(mydict)的结果上转义反斜杠和双引号。至少我以为我会....以下的工作?还是我错过了一个明显的角落?

 >>> s ='''+ json.dumps(mydict).replace('\\','\\\\\'')replace('''''\\'')+' '
>>>打印s
{\a\:1,\b\:\一个带\\\quotes \\\的字符串,\ \t和\\\\\backslashes\}


解决方案

AC字符串以引号开头,以引号结尾,没有嵌入的空值,所有嵌入的引号都以反斜杠转义,所有嵌入的反斜杠文字都翻倍。



所以拿你的字符串,加上反斜杠,并用反斜杠转义引号。我想你的代码正是你所需要的:

  s =''+ json.dumps(mydict) \\',r'\\\'')替换(''',''''''''''

或者,您可以选择稍微不太强大的版本:

  def c_string s)
all_chars =(chr(x)for x in range(256))
trans_table = dict((c,c)for all in all_chars)
trans_table.update({' ':r'\'','\\':r'\\'})
return.join(c中的c为c)

def dwarf_string(d):
import json
return''+ c_string(json.dumps(d))+''
pre>

我喜欢使用 string.maketrans(),但翻译表可以将一个字符映射到最单一的角色。


I have a dictionary in Python that I would like to serialize in JSON and convert to a proper C string so that it contains a valid JSON string that corresponds to my input dictionary. I'm using the result to autogenerate a line in a C source file. Got it? Here's an example:

>>> import json
>>> mydict = {'a':1, 'b': 'a string with "quotes" and \t and \\backslashes'}
>>> json.dumps(mydict)
'{"a": 1, "b": "a string with \\"quotes\\" and \\t and \\\\backslashes"}'
>>> print(json.dumps(mydict))
{"a": 1, "b": "a string with \"quotes\" and \t and \\backslashes"}

What I need to generate is the following C string:

"{\"a\": 1, \"b\": \"a string with \\\"quotes\\\" and \\t and \\\\backslashes\"}"

In other words, I need to escape the backslash and double-quote on the result of calling json.dumps(mydict). At least I think I do.... Will the following work? Or am I missing an obvious corner case?

>>> s = '"'+json.dumps(mydict).replace('\\','\\\\').replace('"','\\"')+'"'
>>> print s
"{\"a\": 1, \"b\": \"a string with \\\"quotes\\\" and \\t and \\\\backslashes\"}"

解决方案

A C string starts with a quote and ends with a quote, has no embedded nulls, has all embedded quotes escaped with backslash, and all embedded backslash literals are doubled.

So take your string, double the backslashes and escape the quotes with a backslash. I think your code is exactly what you need:

s = '"' + json.dumps(mydict).replace('\\', r'\\').replace('"', r'\"') + '"'

Alternatively, you could go for this slightly less robust version:

def c_string(s):
    all_chars = (chr(x) for x in range(256))
    trans_table = dict((c, c) for c in all_chars)
    trans_table.update({'"': r'\"', '\\': r'\\'})
    return "".join(trans_table[c] for c in s)

def dwarf_string(d):
    import json
    return '"' + c_string(json.dumps(d)) + '"'

I'd love to use string.maketrans() but a translation table can map a character to at most a single character.

这篇关于使用Python生成一个C字符串的JSON文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆