Python Forum
UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin
#1
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 ! :)
Reply
#2
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.
Reply
#3
(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 !
Reply
#4
Have you tried afg.write_command(b'DATA EMEM,#14?\xfd?\xfd') ? What is the error message (if any).
Reply
#5
(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...
Reply
#6
Do we agree that you are using Python 3 and not Python 2? If Python 2, try to replace \xfd with \u00fd
Reply
#7
Yes I am using Python 3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Debian] UnicodeEncodeError: 'ascii' codec Winfried 1 218 Nov-16-2022, 11:41 AM
Last Post: Winfried
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 951 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 378 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  UnicodeEncodeError caused by print when program runs from Popen SheeppOSU 5 1,152 Jan-13-2022, 08:11 AM
Last Post: SheeppOSU
  [UnicodeEncodeError from smtplib] yoohooos 0 2,202 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  ASCII-Codec in Python3 [SOLVED] AlphaInc 4 2,426 Jul-07-2021, 07:05 PM
Last Post: AlphaInc
  [solved] unexpected character after line continuation character paul18fr 4 1,716 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  python error: bad character range \|-t at position 12 faustineaiden 0 1,917 May-28-2021, 09:38 AM
Last Post: faustineaiden
  Error with non-ASCII character? giomach 10 3,673 Nov-09-2020, 01:13 PM
Last Post: giomach
  'NoneType' object has no attribute 'encode' bhagyashree 6 5,925 Nov-05-2020, 03:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020