Python equivalent for Ruby's pack function
I'm translating some of my old Ruby scripts into Python and I have trouble finding a function in Python that belongs to Ruby: http://www.ruby-doc.org/core/classes/Array.开发者_运维技巧src/M002222.html - which keeps the array in little endian byte order. Is there any Python module that can help me?
The array module of Python's standard library may offer somewhat-similar functionality (though it's definitely not an exact match). It does not force endianness (you can swap items' endianness with method byteswap
) and does not intrinsically keep items as "a string" (you can convert back and forth with methods tostring
and fromstring
) but it may be worth looking at, depending on how you're using the Ruby pack
function I guess.
The Python memoryview built-in allows you to "cast" an array or any bytes-like object to different endianess. Se the .cast method. The great thing about memoryviews is that they operate without copying bytes: they share memory with the source data structure.
精彩评论