Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.python > #5376
| From | Julian Gethmann <mail.python.org@gethmann.org> |
|---|---|
| Newsgroups | de.comp.lang.python |
| Subject | [Python-de] binary struct in Python |
| Date | 2018-12-20 15:45 +0100 |
| Message-ID | <mailman.32.1545317128.31622.python-de@python.org> (permalink) |
| References | <2456b27e-f194-d6d4-173c-ddc36d255b91@gethmann.org> |
Hallo,
ich habe binäre Daten, die ich gerne in Python Floats umwandeln würde.
Leider bin ich bei der Verwendung von `structs.unpack` über das Problem
gestoßen, dass meine Daten nicht nur aus floats ("f") bestehen, sondern
auch chars ("c") und half-floats ("e") und letztere auf die größe von
floats aufgebläht werden, wenn ich sie in einem structs entpacken will.
Beispiel:
Folgende Daten liegen vor (eigentlich als Datenstrom, also eher 1000 *
data):
```
data =
b'B\x00\x00y\xda\x00\x03\x12\xda\x00\x01!\xe2\x00\x00\x9d\xbb\r\xac\x00\x00\x00\x00\r'
```
Eigentlich könnte man sie mit "cffffefc" lesen, wenn das "c" und "e" als
1 byte und 2 byte groß angenommen würden und nicht in Kombination mit
"f" als 4 byte groß, sodass statt 24 byte 29 erwartet würden.
Gewünscht ist folgendes Verhalten:
```
import struct
def packet_to_data(packet):
Bx, TH, By, Bz = struct.unpack(">ffff", packet[1:17])
TEd = struct.unpack(">e", packet[17:19])[0]
TEa = struct.unpack(">f", packet[19:23])[0]
end = packet[23:24].decode("ascii")
start= packet[:1].decode("ascii")
if packet.startswith(b"B") and end == "\r":
return Bx, By, Bz, TH, TEd, TEa
```
jedoch am Besten als Iterator, wie `struct.iter_unpack("cffffefc", data)`.
Gibt es soetwas, möglichst performant, hättet ihr Vorschläge zum
Optimieren meiner Funktion oder andere Vorschläge?
Vielen Dank und freundliche Grüße,
Julian
Back to de.comp.lang.python | Previous | Next | Find similar
[Python-de] binary struct in Python Julian Gethmann <mail.python.org@gethmann.org> - 2018-12-20 15:45 +0100
csiph-web