我正在开始从 python 到 jython 的迁移过程。以前有人轻松做到过吗? 有哪些陷阱?我应该先在 Jython IDE 中构建然后部署还是什么?
最佳答案
请记住,在 jython 中,在 Java 下运行时,无论您在什么平台上,所有内容都是“Big-Endian”,而在 PC/Linux/Mac(x86) 平台上,python 是 Little Endian。确保在使用 struct.pack 和 struct.unpack 时使用适当的前缀
无前缀
写入数据(enessw.py)
import struct
f = file('tmp.dat', 'wb') # binary
f.write(struct.pack('IIII', 1,2,3,4)) # default endianess
读取数据(enesr.py)
import struct
f = file('tmp.dat', 'rb')
data = f.read()
ints = struct.unpack('IIII', data) # default endianess
print repr(ints)
结果
用 python 编写,用 jython 读取
C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(16777216L, 33554432L, 50331648L, 67108864L)
用 jython 编写,用 python 读取
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)
C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(16777216, 33554432, 50331648, 67108864)
修复
在打包和解包的格式字符串中使用 <。这将指示打包/解包数据的格式特别是小尾数。
使用“
C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)
C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)
引用文献
C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)
C:\Documents and Settings\mat99856\My Documents\tmp>python enessw.py
C:\Documents and Settings\mat99856\My Documents\tmp>python enessr.py
(1, 2, 3, 4)
C:\Documents and Settings\mat99856\My Documents\tmp>jython enessr.py
(1L, 2L, 3L, 4L)
引用文献
关于Python 到 Jython 的陷阱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812362/