Editing bytes in Python

Preface: I am extremely new to Python, so this question may be asked poorly.

I am working with an API that returns a buffer which I need to edit and then pass back into another function.  In the API documentation, buffer is shown as the return data type.  Which brings me to my first question:  what exactly is a buffer datatype?

If I treat the buffer as a bytearray and edit it, I get the following error:

'str' object does not support item assignment

Googling led me to believe the buffer may actually be a bytes datatype.  If this is indeed the case, how can I edit the nth byte in the buffer of bytes?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First of all, please, tell what version of Python you use. It could be Python 2 (because of this https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange), but it also can be Python 3 that knows the bytes type and also knows a buffer protocol.

Anyway, it also looks that you try to assign a character or byte into a string like this s[0] = 'a', but that is not possible in Python. Python strings are immutable which means that once the value is assigned, it is constant. The same holds for the bytes type. If you want to modify it, you need bytearray (in Python 3).

You should post at least a snippet of your code. Try (to understand) the following:

buf = b'\x01\x02\x03\x04\xff'
print(buf)      # representation
print(buf[2])   # bytes are small integers

buffer = bytearray(buf) # use it as initializer of the mutable structure

# Change the third byte.
buffer[2] = 128

# Convert it back to bytes.
buf2 = bytes(buffer)

# Anyway, assignment to bytes type fails.
# And similarly, assignment to str fails
##buf[2] = 100  # uncomment this
s = 'abc'
s[1] = 'x'

Open in new window

It prints...
Traceback (most recent call last):
  File "D:\__Python\efryevt\ee28735170\a.py", line 24, in <module>
    s[1] = 'x'
TypeError: 'str' object does not support item assignment

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Suhas .Senior QA ManagerCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: pepr (https:#a41018051)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.