1

I am working with python3 and I have a problem with the representation of an object.
It is the result of subprocess.Open().communicate()[0], which is a bytes-array but when I pass it to another function, python interprets it as a string, which is not.
This is the array that i have:

b'N\x00e\x00l\x00 \x00s\x00o\x00t\x00t\x00o\x00s\x00i\x00s\x00t\x00e\x00m\x00a\x00 \x00W\x00i\x00n\x00d\x00o\x00w\x00s\x00 \x00p\x00e\x00r\x00 \x00L\x00i\x00n\x00u\x00x\x00 \x00n\x00o\x00n\x00 \x00s\x00o\x00n\x00o\x00 \x00i\x00n\x00s\x00t\x00a\x00l\x00l\x00a\x00t\x00e\x00 \x00d\x00i\x00s\x00t\x00r\x00i\x00b\x00u\x00z\x00i\x00o\x00n\x00i\x00.\x00\r\x00\r\x00\n\x00L\x00e\x00 \x00d\x00i\x00s\x00t\x00r\x00i\x00b\x00u\x00z\x00i\x00o\x00n\x00i\x00 \x00p\x00o\x00s\x00s\x00o\x00n\x00o\x00 \x00e\x00s\x00s\x00e\x00r\x00e\x00 \x00i\x00n\x00s\x00t\x00a\x00l\x00l\x00a\x00t\x00e\x00 \x00v\x00i\x00s\x00i\x00t\x00a\x00n\x00d\x00o\x00 \x00M\x00i\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00 \x00S\x00t\x00o\x00r\x00e\x00:\x00\r\x00\r\x00\n\x00h\x00t\x00t\x00p\x00s\x00:\x00/\x00/\x00a\x00k\x00a\x00.\x00m\x00s\x00/\x00w\x00s\x00l\x00s\x00t\x00o\x00r\x00e\x00\r\x00\r\x00\n\x00'

Which is in ASCII encode, right?

My question is how can I encode it to see the right bytes values?
Or is there at least a way to pass it to the other function without it been recognised as a string, which is not?

Thanks!

1

2 Answers 2

0

You can decode it with .decode('utf-8') To remove the null bytes use .replace(b'\x00', b'')

subprocessoutput = b'N\x00e\x00l\x00 \x00s\x00o\x00t\x00t\x00o\x00s\x00i\x00s\x00t\x00e\x00m\x00a\x00 \x00W\x00i\x00n\x00d\x00o\x00w\x00s\x00 \x00p\x00e\x00r\x00 \x00L\x00i\x00n\x00u\x00x\x00 \x00n\x00o\x00n\x00 \x00s\x00o\x00n\x00o\x00 \x00i\x00n\x00s\x00t\x00a\x00l\x00l\x00a\x00t\x00e\x00 \x00d\x00i\x00s\x00t\x00r\x00i\x00b\x00u\x00z\x00i\x00o\x00n\x00i\x00.\x00\r\x00\r\x00\n\x00L\x00e\x00 \x00d\x00i\x00s\x00t\x00r\x00i\x00b\x00u\x00z\x00i\x00o\x00n\x00i\x00 \x00p\x00o\x00s\x00s\x00o\x00n\x00o\x00 \x00e\x00s\x00s\x00e\x00r\x00e\x00 \x00i\x00n\x00s\x00t\x00a\x00l\x00l\x00a\x00t\x00e\x00 \x00v\x00i\x00s\x00i\x00t\x00a\x00n\x00d\x00o\x00 \x00M\x00i\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00 \x00S\x00t\x00o\x00r\x00e\x00:\x00\r\x00\r\x00\n\x00h\x00t\x00t\x00p\x00s\x00:\x00/\x00/\x00a\x00k\x00a\x00.\x00m\x00s\x00/\x00w\x00s\x00l\x00s\x00t\x00o\x00r\x00e\x00\r\x00\r\x00\n\x00' print(subprocessoutput.replace(b'\x00', b'').decode('utf-8')) 

edit: the following creates a list with the ascii numbers in decimal:

ascii = [ord(x) for x in subprocessoutput.decode('utf-8')] 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your reply but the problem is still here. I've checked online and really the encoding of this ASCII string is a series of numbers, exactly what i need, but i cannot seem to obtain it on python
0

The problem is this isn't UTF-8, it's UTF-16.

#!/bin/python3 var = b'N\x00e\x00l\x00 \x00s\x00o\x00t\x00t\x00o\x00s\x00i\x00s\x00t\x00e\x00m\x00a\x00 \x00W\x00i\x00n\x00d\x00o\x00w\x00s\x00 \x00p\x00e\x00r\x00 \x00L\x00i\x00n\x00u\x00x\x00 \x00n\x00o\x00n\x00 \x00s\x00o\x00n\x00o\x00 \x00i\x00n\x00s\x00t\x00a\x00l\x00l\x00a\x00t\x00e\x00 \x00d\x00i\x00s\x00t\x00r\x00i\x00b\x00u\x00z\x00i\x00o\x00n\x00i\x00.\x00\r\x00\r\x00\n\x00L\x00e\x00 \x00d\x00i\x00s\x00t\x00r\x00i\x00b\x00u\x00z\x00i\x00o\x00n\x00i\x00 \x00p\x00o\x00s\x00s\x00o\x00n\x00o\x00 \x00e\x00s\x00s\x00e\x00r\x00e\x00 \x00i\x00n\x00s\x00t\x00a\x00l\x00l\x00a\x00t\x00e\x00 \x00v\x00i\x00s\x00i\x00t\x00a\x00n\x00d\x00o\x00 \x00M\x00i\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00 \x00S\x00t\x00o\x00r\x00e\x00:\x00\r\x00\r\x00\n\x00h\x00t\x00t\x00p\x00s\x00:\x00/\x00/\x00a\x00k\x00a\x00.\x00m\x00s\x00/\x00w\x00s\x00l\x00s\x00t\x00o\x00r\x00e\x00\r\x00\r\x00\n\x00' foo = str(var, 'utf-16') print(foo) 

This yields

Nel sottosistema Windows per Linux non sono installate distribuzioni. Le distribuzioni possono essere installate visitando Microsoft Store: https://aka.ms/wslstore

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.