Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101758
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | jmp <jeanmichel@sequans.com> |
| Newsgroups | comp.lang.python |
| Subject | Writing a stream of bytes |
| Date | Fri, 15 Jan 2016 16:55:08 +0100 |
| Lines | 38 |
| Message-ID | <mailman.16.1452873321.15297.python-list@python.org> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de XCyAn6jOnYk4AOOhGFrcSgrbyxKcPwKAJ8VFyTlGC60w== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'bytes.': 0.07; '(instead': 0.09; 'endian': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Writing': 0.09; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'bytes': 0.18; 'tells': 0.18; 'file:': 0.22; 'struct': 0.22; 'cheers,': 0.22; 'file.': 0.22; 'seems': 0.23; 'import': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'actual': 0.28; "i'd": 0.31; 'maybe': 0.33; 'stream': 0.33; 'file': 0.34; 'but': 0.36; 'to:addr:python-list': 0.36; 'say': 0.37; 'received:org': 0.37; 'end': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'received:194': 0.61; 'more': 0.63; 'note:': 0.66; 'order:': 0.84; 'overlooked': 0.84; 'struct.': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | paris.sequans.com |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:101758 |
Show key headers only | View raw
Hi pyple !
I'd like to write a stream of bytes into a file. I'd like to use the
struct (instead of bytearray) module because I will have to write more
than bytes.
let's say I want a file with 4 bytes in that order:
01 02 03 04
None of these work:
import struct
with open('toto', 'wb') as f: f.write(struct.pack('4B', *[1,2,3,4]))
with open('toto', 'wb') as f: f.write(struct.pack('<4B', *[1,2,3,4]))
with open('toto', 'wb') as f: f.write(struct.pack('>4B', *[1,2,3,4]))
I always end up with the following bytes on file:
!hexdump toto
0000000 0201 0403
Note: the '<' and '>' tells struct to pack for a litle/big endian
architecture.
The only solution I came up with is
with open('toto', 'wb') as f: f.write(struct.pack('>4B', *[2,1,4,3]))
But I'd rather not manipulate the stream, as it seems to me that this
would be the job of struct. Or maybe I completely overlooked the actual
issue ?
Cheers,
jm
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Writing a stream of bytes jmp <jeanmichel@sequans.com> - 2016-01-15 16:55 +0100 Re: Writing a stream of bytes BartC <bc@freeuk.com> - 2016-01-15 16:04 +0000
csiph-web