This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Struct module should accept arrays
Type: enhancement Stage:
Components: Versions: Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gamaanderson, mark.dickinson, meador.inge
Priority: normal Keywords:

Created on 2015-04-13 14:25 by gamaanderson, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg240610 - (view) Author: Anderson (gamaanderson) Date: 2015-04-13 14:25
Correct me if I'm wrong, the struct module does not work with array of ints, floats etc (just work with char in the form of strings). I think it should since this are valid elements in C structs. 

More specifically, consider I have this C struct

 int array[4];

I'm forced to do something like this:
  struct.pack('iiii', v1,v2,v3,v4)  #'4i' is just the same as 'iiii'

I would like to do something like this:
  struct.pack('i[4]', [v1,v2,v3,v4])

Of course this is useful if I want to pack with zeros:
  struct.pack('i[4]', [0]*4)
msg240648 - (view) Author: Wolfgang Maier (wolma) * Date: 2015-04-13 16:33
are you looking for:

struct.pack('i'*4, *[0]*4)

msg240658 - (view) Author: Anderson (gamaanderson) Date: 2015-04-13 16:52
@wolma, That would work in this simple example. But in a more complicated case this became inconvenient.

Actually I'm working with reading and writing in python an extremely C-oriented file-type (MDV). For that I represent C-structs as python dics (e.q {"array":[v1,v2,v3,v4]}), and because of this lack of arrays I'm forced to keep track if a key represents a single value or an array. It works, but I think everyone would benefit if struct could handle that simple task.
msg240714 - (view) Author: Wolfgang Maier (wolma) * Date: 2015-04-13 19:41
I'm afraid you lost me and I do not see what your problem is here.
Maybe you should raise this on one of the Python mailing lists (see ?
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68121
2019-08-25 23:43:44aerossetnosy: + mark.dickinson, meador.inge
2019-08-25 23:25:41nanjekyejoannahsettitle: Struct module should acept arrays -> Struct module should accept arrays
2015-04-13 19:43:03wolmasetnosy: - wolma
2015-04-13 19:41:25wolmasetmessages: + msg240714
2015-04-13 16:52:20gamaandersonsetmessages: + msg240658
2015-04-13 16:33:44wolmasetnosy: + wolma
messages: + msg240648
2015-04-13 14:25:51gamaandersoncreate