python convert memoryview to string

Viewed 12944

I tried to search everywhere but could not find any relevant info.

As a result of the following code:

overlapped = pywintypes.OVERLAPPED()
buffer = win32file.AllocateReadBuffer(1024*4)
fullDataRead = []
hr, data = win32file.ReadFile(handle, buffer, overlapped)
n = win32file.GetOverlappedResult(handle, overlapped, 1)
read = str(data[:n])
fullDataRead.append(read)
print(fullDataRead)

I get

['<memory at 0x0000026821801348>']

but I need strings or bytes that are inside. Any ideas how to read a memoryview object? Thank you

2 Answers

For Python 2.7 and 3.x you can try this:

a = memoryview(b'mystring')
print(a)  # <memory at 0x10cbebb98>
#for string
print(str(a,'utf8'))  # 'mystring' as UTF-8

# for bytes
print(bytes(a))  # b'mystring'
Related