我正在使用一个小型 Python 脚本来生成一些将在 C 标头中使用的二进制数据。
此数据应声明为
char[]
,如果可以将其编码为字符串(当它们不在 ASCII 可打印字符范围内时,使用相关的转义序列)以保持标头比使用十进制或十六进制数组编码。
问题是,当我打印 Python 字符串的
repr
时,它是用单引号分隔的,而 C 不喜欢这样。天真的解决方案是:
'"%s"'%repr(data)[1:-1]
但是当数据中的一个字节恰好是双引号时,这不起作用,所以我也需要将它们转义。
我认为一个简单的
replace('"', '\\"')
就可以完成这项工作,但也许有更好、更Pythonic的解决方案。
加分:
将数据分割成大约 80 个字符的行也很方便,但同样,将源字符串分割成大小为 80 的块的简单方法行不通,因为每个不可打印的字符需要 2 或 3 个字符转义序列。在获取 repr 之后将列表拆分为 80 个块也无济于事,因为它可能会划分转义序列。 有什么建议吗?
json.dumps
:
>>> import json
>>> print(json.dumps("hello world"))
"hello world"
>>> print(json.dumps('hëllo "world"!'))
"h\u00ebllo \"world\"!"
我不确定 json 字符串是否与 C 兼容,但至少它们有一个相当大的公共子集,并且保证与 javascript 兼容;)。
repr()
,而是从一开始就使用正确的编码。您可以直接使用编码来获取repr的编码
string_escape
>>> "naïveté".encode("string_escape")
'na\\xc3\\xafvet\\xc3\\xa9'
>>> print _
na\xc3\xafvet\xc3\xa9
为了转义“-引号,我认为在对字符串进行转义编码后使用简单的替换是一个完全明确的过程:
>>> '"%s"' % 'data:\x00\x01 "like this"'.encode("string_escape").replace('"', r'\"')
'"data:\\x00\\x01 \\"like this\\""'
>>> print _
"data:\x00\x01 \"like this\""
str
询问其
repr
,我认为引用类型并不是真正可配置的。来自 python 2.6.4 源代码树中的 PyString_Repr
函数: /* figure out which quote to use; single is preferred */
quote = '\'';
if (smartquotes &&
memchr(op->ob_sval, '\'', Py_SIZE(op)) &&
!memchr(op->ob_sval, '"', Py_SIZE(op)))
quote = '"';
所以,我想如果字符串中有单引号就使用双引号,但如果字符串中有双引号就不要使用。
我会尝试编写自己的类来包含字符串数据,而不是使用内置字符串来执行此操作。一种选择是从
str
派生一个类并编写自己的
repr
:class MyString(str):
__slots__ = []
def __repr__(self):
return '"%s"' % self.replace('"', r'\"')
print repr(MyString(r'foo"bar'))
或者,根本不要使用
repr
:
def ready_string(string):
return '"%s"' % string.replace('"', r'\"')
print ready_string(r'foo"bar')
如果字符串中已经存在转义引号,这种简单的引用可能不会做“正确”的事情。
这段代码可能是正确的方向。我使用的默认值是 140 列,这对于 2009 年来说是一个合理的值,但如果您确实想将代码包装到 80 列,只需更改它即可。
如果unicode=True,则输出一个L“宽”字符串,该字符串可以有意义地存储Unicode转义符。或者,您可能希望将 Unicode 字符转换为 UTF-8 并将其转义输出,具体取决于您在其中使用它们的程序。
def string_to_c(s, max_length = 140, unicode=False):
ret = []
# Try to split on whitespace, not in the middle of a word.
split_at_space_pos = max_length - 10
if split_at_space_pos < 10:
split_at_space_pos = None
position = 0
if unicode:
position += 1
ret.append('L')
ret.append('"')
position += 1
for c in s:
newline = False
if c == "\n":
to_add = "\\\n"
newline = True
elif ord(c) < 32 or 0x80 <= ord(c) <= 0xff:
to_add = "\\x%02x" % ord(c)
elif ord(c) > 0xff:
if not unicode:
raise ValueError, "string contains unicode character but unicode=False"
to_add = "\\u%04x" % ord(c)
elif "\\\"".find(c) != -1:
to_add = "\\%c" % c
else:
to_add = c
ret.append(to_add)
position += len(to_add)
if newline:
position = 0
if split_at_space_pos is not None and position >= split_at_space_pos and " \t".find(c) != -1:
ret.append("\\\n")
position = 0
elif position >= max_length:
ret.append("\\\n")
position = 0
ret.append('"')
return "".join(ret)
print string_to_c("testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing testing", max_length = 20)
print string_to_c("Escapes: \"quote\" \\backslash\\ \x00 \x1f testing \x80 \xff")
print string_to_c(u"Unicode: \u1234", unicode=True)
print string_to_c("""New
lines""")