Python3 bytearray padding


 
Thread Tools Search this Thread
Top Forums Programming Python3 bytearray padding
# 1  
Old 07-31-2010
Python3 bytearray padding

Hi,
I'm trying to create a padded bytearray in Python3.
Currently I have something like this:

Code:
inputstuff = [
('A' , b'ABCD'),
('B' , b'EFGH'),
('C' , b'IJKL'),
('D' , b'MNOP')
]
BA = bytearray()

for thing in inputstuff:
        BA.extend(thing[1])
 
print(BA.decode('latin1'))
print(BA)
 
for thing in BA:
        print(thing)

This works and gives output like this:

Code:
ABCDEFGHIJKLMNOP
bytearray(b'ABCDEFGHIJKLMNOP')
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

Which is expected.

However, what i'd actually like to do is pad the bytes I add to the bytearray with blanks such that it would be more like this:

ABCD EFGH IJKL MNOP

Obviously I could just add spaces to the end of each string defined as A, B, C and D - but i'd rather not do that as it would get complicated when I use this code in a real scenario where these are objects being passed into the function etc.

If anybody knows how to do this i'd be very grateful of your help.

Regards,

Phil

Last edited by pludi; 08-01-2010 at 09:16 AM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to convert bytearray into text2pcap format

Hi, I'm looking for a method to add hex increments as a first column to the following file. Something like this: 0000 0010 0020 0030 0040 0050 0060 0070 0080 0090 00a0 00b0 00c0 00d0 (5 Replies)
Discussion started by: sand1234
5 Replies

2. UNIX for Beginners Questions & Answers

Make throwing error while compiling python3.6 on Solaris10

Hi, I have downloaded gzipped source tarball of python3.7. I have tried to compile like below, ./configure --prefix=/directory Compiling seems to be ok..But when running make all..Getting too many missing initializer warnings /python3.6/Python-3.6.0/Modules/_cursesmodule.c: At top level:... (1 Reply)
Discussion started by: Sumanthsv
1 Replies

3. Shell Programming and Scripting

Python3 subprocess troubles

After struggling with this for days now, I'm reaching out to the experts of all things linux for some help with this. I'm trying to run the following working command (on command line) inside a python script using subprocess: rsync -avzh --no-perms --delete --include="*sub*" --exclude='*'... (2 Replies)
Discussion started by: timj123
2 Replies

4. Shell Programming and Scripting

bash padding

Hi all Is there a way to pad the output of a bash script see that code below for i in `sed -n '/Start Printer/,/End Printer/p' /u/ab/scripts/hosts.conf | awk '!/^#/ {print $2}' | egrep -v 'broke|primera' `; do pages=`snmpget -Ov -v1 -c public $i sysLocation.0 | awk '{print $2}'` ... (3 Replies)
Discussion started by: ab52
3 Replies

5. Shell Programming and Scripting

Padding leading zero

hi All i am new to linux... source txt .. 281-BUM-5M BUM-5M 0 0 282-BUM-5M BUM-5M 0 0 83-BUM-5M BUM-5M 0 0 is it possible to use bash script to convert to (remove the "-" and fill up to 4 digit" ? 0281 BUM-5M BUM-5M 0 0 0282 BUM-5M BUM-5M 0 0 0083 BUM-5M BUM-5M 0 0 thanks a ... (5 Replies)
Discussion started by: samoptimus
5 Replies

6. UNIX for Dummies Questions & Answers

Zero padding dates

I have a file with records containing dates like: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|2007-6-6|MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|2003-11-6|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| How can I get the date fields in each of my records to be... (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

7. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

8. Shell Programming and Scripting

Padding in Unix

I have a file with different character counts on each line how do i make it with unique character counts. example: 1st line : ABCD 011 XYZ 0000 YYYY BBB TEADINGDA 2nd line: ABCD 011 xys 0010 YYYY BBB TEAD 3rd line : ABCD 022 YXU 000 UUU BBB TE 1st line is 43... (3 Replies)
Discussion started by: rudoraj
3 Replies

9. UNIX for Dummies Questions & Answers

Padding

Hi Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting Your answer is very much appreciated Thanks Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies

10. UNIX for Advanced & Expert Users

Padding issues

Hello, Structure padding & structure size are different on Compaq & HP UNIX. When structures are transfered via netfork from Compaq to HP will this be a problem? If yes, what can be the solution? Thanks, shilpa (2 Replies)
Discussion started by: informshilpa
2 Replies
Login or Register to Ask a Question