【发布时间】:2020-10-14 03:17:36
【问题描述】:
我有 python 字典:
data = {
'foo': 'bar',
'hello': 'world'
}
如何将这个 dict 打包成 XDR 数据格式?
【问题讨论】:
标签: python python-2.7 python-3.x xdr
我有 python 字典:
data = {
'foo': 'bar',
'hello': 'world'
}
如何将这个 dict 打包成 XDR 数据格式?
【问题讨论】:
标签: python python-2.7 python-3.x xdr
这实际上取决于您希望它如何包装。作为 key value 对,您可以使用以下内容:
Python 2.x:
import xdrlib
data = {'foo': 'bar', 'hello': 'world'}
p = xdrlib.Packer()
for key, value in data.items():
p.pack_string(key)
p.pack_string(value)
print p.get_buffer()
Python 3.x:
import xdrlib
data = {'foo': 'bar', 'hello': 'world'}
p = xdrlib.Packer()
for key, value in data.items():
p.pack_string(key.encode())
p.pack_string(value.encode())
print(p.get_buffer())
会显示如下内容:
◦◦◦foo◦◦◦◦bar◦◦◦◦hello◦◦◦◦◦◦world◦◦◦
【讨论】: