Vladislav Gladkikh
Vladislav Gladkikh

Reputation: 407

How to convert an encoded string to a readable format?

I import a mat file with scipy.io.loadmat and one of the variables is a numpy.ndarray of strings that look like

'a\x00\x00\x00cytns\x00\x00\x00\x00\x00o\x00\x00\x00cy\x00ie\x00z-\x00\x00\x00\x00\x00-\x00\x00\x00\x00\x00\x00u\x00\x00\x00t\x00\x00z'

How to convert them to a readable format in Python3?

Upvotes: 2

Views: 1285

Answers (1)

Rakesh
Rakesh

Reputation: 82765

python3 Example

The StringIO module might help in python2.

Demo:

# -*- coding: utf-8 -*-
import StringIO
s = 'a\x00\x00\x00cytns\x00\x00\x00\x00\x00o\x00\x00\x00cy\x00ie\x00z-\x00\x00\x00\x00\x00-\x00\x00\x00\x00\x00\x00u\x00\x00\x00t\x00\x00z'
print StringIO.StringIO(s).getvalue()

# using decode method
print s.decode('utf-8')

Output:

acytnsocyiez--utz
acytnsocyiez--utz

Upvotes: 3

Related Questions