I'm using struct.unpack to read the 11th byte of a file to the 21st byte which represents a field that is supposed to read 'SNA'. The field is 'populated as BCS-A where it is left justified and padded to the right boundary with BCS spaces'. Since the field is 10 bytes long, my format string is '10s'. However, per the output mentioned, the remaining 7 bytes are spaces. To eliminate those spaces I use strip. Unfortunately, this still yields 'SNA\x00'. What am I doing wrong?
field = struct.unpack('10s',data[start:stop])
field[0].strip() (since the output of a strut.unpack is a tuple)
-
I can't find a reference for "BCS-A". Can you elaborate on what that term means to you?Robᵩ– Robᵩ2015年05月01日 21:36:55 +00:00Commented May 1, 2015 at 21:36
-
Basic Character Set-Alphanumeric (BCS-A). A subset of the Basic Character Set. The range of allowable characters consists of space to tilde, codes 0x20 to 0x7E.greg– greg2015年05月01日 21:39:31 +00:00Commented May 1, 2015 at 21:39
1 Answer 1
Your data doesn't conform to the standard you've specified. Either contact your data supplier and have them fix their bug, or be more generous about your definition of "space". If you want to accept that data, you could, for example, do this:
field[0].strip(' \t\n\x00')
or, with more limited acceptance:
field[0].strip().rstrip('\x00')