Python Forum
UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin (/thread-37073.html)



UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - Armandito - Apr-29-2022

Hey everyone !

I have a strange problem. I would like to send this line to a waveform generator: 'DATA EMEM,#14?\xfd?\xfd'.

I get this error : UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordinal not in range(128)

I found a lot of topics on the Internet to solve this problem, and the solution seems to be to encode the string with .encode('utf-8').

The problem is that encoding with this function puts a b'[my_code]' around my code, and when I send it to my device, it obviously doesn't understand it because it doesn't expect a binary table but a string ... So I try to convert it to a string but I come back to the first problem ....

How can I keep the encoding while deleting this b' ' around my string ?

I hope my problem is quite clear and that someone can help me !
Thanks a lot ! :)


RE: UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - supuflounder - Apr-29-2022

Can you show the actual code you are using? All we need are the open and print calls, plus the way you form the data to be sent. I haven't looked at it in a while, but there is a way to tell Python to not encode a string to a device.


RE: UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - Armandito - Apr-29-2022

(Apr-29-2022, 09:30 AM)supuflounder Wrote: Can you show the actual code you are using? All we need are the open and print calls, plus the way you form the data to be sent. I haven't looked at it in a while, but there is a way to tell Python to not encode a string to a device.

The line where is the prroblem is that one : afg.write_command('DATA EMEM,#14?\xfd?\xfd')

and the function write_command is this one :

def write_command(self,str_command):
rm = pyvisa.ResourceManager(r'C:\WINDOWS\System32\nivisa64.dll')
instrument = rm.open_resource(self.ins_name)
instrument.write(str_command)
instrument.close()
rm.close()



where ins_name is the name of the connection between my computer and the device

I hope I gave you everything you need !


RE: UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - Gribouillis - Apr-29-2022

Have you tried afg.write_command(b'DATA EMEM,#14?\xfd?\xfd') ? What is the error message (if any).


RE: UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - Armandito - Apr-29-2022

(Apr-29-2022, 12:02 PM)Gribouillis Wrote: Have you tried afg.write_command(b'DATA EMEM,#14?\xfd?\xfd') ? What is the error message (if any).

Yes i tried and it does not work : TypeError: endswith first arg must be bytes or a tuple of bytes, not str
The fact is that the first part of my command ( DATA EMEM,#14) is supposed to be a string so I cannot convert it into bytes...


RE: UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - Gribouillis - Apr-29-2022

Do we agree that you are using Python 3 and not Python 2? If Python 2, try to replace \xfd with \u00fd


RE: UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin - Armandito - Apr-29-2022

Yes I am using Python 3