Sending corrupted csv line over the socket in Python 3 -
i build sensor unit data gathered @ 1 raspberry pi , send another's on network. first pi creates line multiple readings different sensors. supposed create server , send clients. client pis needs receive sentence, further processing or visualisation.
to test solutions want read data txt file, build in experiment. problem data corrupted, has different format depending on sensor , rows can different different set-ups.
i have build function suppose change input line bytes. (i tried different methods clunky function closest results). not convert on network
import struct message = ['first sensor', 'second data', 'third',1, '19.04.2016', 0.1] def packerfornet(message): pattern = '' newmessage = [] cell in message: if isinstance( cell, int ): pattern += ('i') newmessage.append(cell) elif isinstance( cell, float ): pattern += ('d') newmessage.append(cell) elif isinstance(cell, str): pattern += (str(len(cell))) pattern += ('s') newmessage.append(cell.encode('utf-8')) else: cell = str(cell) pattern += (len(cell)) pattern += ('s') newmessage.append(cell.encode('utf-8')) return (newmessage, pattern) newmessage, pattern = packerfornet(message) patternstruct = struct.struct(pattern) packedm = patternstruct.pack(*newmessage)
the output function not unpack correctly:
packedm = b'first sensorsecond datathird\x01\x00\x00\x0019.04.2016\x00\x00\x00\x00\x00\x00\x9a\x99\x99\x99\x99\x99\xb9?' 56 print('unpacked = %s' % patternstruct.unpack(packedm)) typeerror: not arguments converted during string formatting
in addition need know pattern
unpack on client side in general doesn't have sense.
in final version server needs work in way after client connects send client line sensors every millisecond. sensors parsing implemented in c , @ moment creates txt file off-line processing. can't change way sensor's line made.
i don't know how pack list of different types , send such lists client.
actually unpack correctly. problem not unpacking, print.
try:
unpackedm = patternstruct.unpack(packedm) print(unpackedm)
unpackedm tuple of multiple values. formatting of string tuple failed.
edit: convert entire objects can use python msgpack. use in communication python python , php python.
Comments
Post a Comment