Method GetUserPhotosRequest for file_reference - python-3.x

I try get file_file_reference, but can't decode result
with TelegramClient('anon', api_id, api_hash) as client:
result = client(functions.photos.GetUserPhotosRequest(
user_id='******',
offset=0,
max_id=0,
limit=0
))
array_of_photos = result.photos
for photo in array_of_photos:
usert_photo = photo.file_reference
print(usert_photo.decode('utf-8'))
Result:
b'\x00_F\x1a:\xc6\x1e\x9e\xc0\xd9\x18\x95\xff\x02\x01r\xff8d\xf2\xb9'
b"\x00_F\x18\xb6\xd51\xba\x1ek\x17\x0c\xc6I\x15\x96\xb4\x89\xb3\xa6'"
b'\x00_F\x18\xa6\xc0\xf72*FN\xe0|\x11\x9f+M\xfe\xe8Ab'
Error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 7: invalid start byte

That reference can't be decoded to string, and shall be stored as bytes.

Related

pyserial response unicodeDecodeError

import serial
s = serial.Serial(port = 'Com3', 9600, timeout = 2)
data = s.readline().decode().rstrip("\r\n")
So basically when I try to read the data I get the error.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfb in position 20: invalid start byte
From the documentation of the instrument I am trying to communicate with. the data is in the form:
..ss3422/34/54--1.8E+03<,>…..0.7E+03<,...1.71E-09<,<√.<*.<
I was able to fix the problem by changing to encoding = "ISO-8859-1".

How to convert characters into bytes-like object for Python3 base64 encoding?

I have a function in Python2 which encrypts a string into a base64 encoded object.
When I run the same in Python3 I get different output. Together with the following TypeError:
TypeError: a bytes-like object is required, not 'str'
How can I get a the correct bytes-like object in order to encode the string "hello world" into base64?
enc = []
clear = "hello world"
key = "1234567890"
for i in range(len(clear)):
key_c = key[i % len(key)]
enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
enc.append(enc_c)
print ("enc:", enc)
print (base64.urlsafe_b64encode("".join(enc)))
Running this in Python2, gives the following output:
enc: ['\x99', '\x97', '\x9f', '\xa0', '\xa4', 'V', '\xae', '\xa7', '\xab', '\x9c', '\x95']
base64 encoded: mZefoKRWrqernJU=
Running this in Python3, gives different output for the encrypted list enc and the TypeError:
enc: ['\x99', '\x97', '\x9f', '\xa0', '¤', 'V', '®', '§', '«', '\x9c', '\x95']
How can I get same the results running in Python3?
Based on the explanation and example code provided by topaco, this is the working encoding function:
def encode(key, clear):
enc = []
bkey = key.encode()
bclear = clear.encode()
for i in range(len(bclear)):
key_c = bkey[i % len(bkey)]
enc_c = (bclear[i] + key_c) % 256
enc.append(enc_c)
encB64 = base64.urlsafe_b64encode(bytes(enc)) # convert list to bytes-like object
print("base64 encoded:", encB64.decode())
return encB64.decode()

How to decode payload from modbus using NodeJS?

I have a schneider power meter with rs485 surport. I using python with pymodbus to read register and decode payload from it (success). But now I want to do this with NodeJS, I can get raw data but I dont know how to decode it, I tried some method but result wrong!
This my python code:
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
def validator(instance):
if not instance.isError():
'''.isError() implemented in pymodbus 1.4.0 and above.'''
decoder = BinaryPayloadDecoder.fromRegisters(
instance.registers,
byteorder=Endian.Big, wordorder=Endian.Little
)
return float(decoder.decode_32bit_float())
else:
# Error handling.
return None
validator([5658, 17242]) # Result is 218.1
When I use NodeJS it return buffer and i tried to decode with:
let buf = Buffer.from([0xd6, 0xd4, 0x42, 0x47]);
payload = buf.readFloatBE(0); // It return other float number not 218.1
Can everyone help me ! Thanks !

Decoding error while decoding stdout from subprocess.Popen

string.decode() throws an error, when i try to decode the line output of an stdout.PIPE. The error message is:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x84 in position 8: invalid start byte
0x84 should be the letter 'ä'. The line that fails reads as follows:
b' Datentr\x84ger in Laufwerk C: ist System'
I can't nail it down. I already checked the encoding using sys.stdout.encoding, which is utf-8.
import subprocess
import re
prc = subprocess.Popen(["cmd.exe"], shell = False, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
prc.stdin.write(b"dir\n")
outp, inp = prc.communicate()
regex = re.compile(r"^.*(\d\d:\d\d).*$")
for line in outp.splitlines():
match = regex.match(line.decode('utf-8'))# <--- decode fails here.
if match:
print(match.groups())
prc.stdin.close()
CMD encodes text using ISO-8859-15. So the text that comes through the PIPE needs to be decoded using ISO, even if python encodes the stdout using utf-8.
If you don’t know the encoding, the cleanest way to solve this is to specify the errors param of bytearray.decode, e.g.:
import subprocess
p = subprocess.run(['echo', b'Evil byte: \xe2'], stdout=subprocess.PIPE)
p.stdout.decode(errors='backslashreplace')
Output:
'Evil byte: \\xe2\n'
The list of possible values can be found here:
https://docs.python.org/3/library/codecs.html#codecs.register_error

load .npy file from google cloud storage with tensorflow

i'm trying to load .npy files from my google cloud storage to my model i followed this example here Load numpy array in google-cloud-ml job
but i get this error
'utf-8' codec can't decode byte 0x93 in
position 0: invalid start byte
can you help me please ??
here is sample from the code
Here i read the file
with file_io.FileIO(metadata_filename, 'r') as f:
self._metadata = [line.strip().split('|') for line in f]
and here i start processing on it
if self._offset >= len(self._metadata):
self._offset = 0
random.shuffle(self._metadata)
meta = self._metadata[self._offset]
self._offset += 1
text = meta[3]
if self._cmudict and random.random() < _p_cmudict:
text = ' '.join([self._maybe_get_arpabet(word) for word in text.split(' ')])
input_data = np.asarray(text_to_sequence(text, self._cleaner_names), dtype=np.int32)
f = StringIO(file_io.read_file_to_string(
os.path.join('gs://path',meta[0]))
linear_target = tf.Variable(initial_value=np.load(f), name='linear_target')
s = StringIO(file_io.read_file_to_string(
os.path.join('gs://path',meta[1])))
mel_target = tf.Variable(initial_value=np.load(s), name='mel_target')
return (input_data, mel_target, linear_target, len(linear_target))
and this is a sample from the data sample
This is likely because your file doesn't contain utf-8 encoded text.
Its possible, you may need to initialize the file_io.FileIO instance as a binary file using mode = 'rb', or set binary_mode = True in the call to read_file_to_string.
This will cause data that is read to be returned as a sequence of bytes, rather than a string.

Resources