Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

Exception has occurred: ValueError string argument should contain only ASCII characters…

  1. User

    Exception has occurred: ValueError string argument should contain only ASCII characters UnicodeEncodeError: 'ascii' codec can't encode character '\x9c' in position 1: ordinal not in range(128)

    During handling of the above exception, another exception occurred:

    File "C:\Users\Brahim\Desktop\Python\WoW\TTS\qr-detect.py", line 29, in process_qrcode_data compressed_data = base64.b64decode(text) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Brahim\Desktop\Python\WoW\TTS\qr-detect.py", line 108, in test_qr_detection_tts text_to_read, npc_name, npc_sex, query_type = self.process_qrcode_data(qr_data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Brahim\Desktop\Python\WoW\TTS\qr-detect.py", line 149, in asyncio.run(qr_detector.test_qr_detection_tts()) ValueError: string argument should contain only ASCII characters

  2. Assistant

    The error message you are receiving indicates that there's a problem with string encoding in your program. The Python base64 module expects a base64-encoded ASCII string for the b64decode function, but it seems that the string passed to the base64.b64decode(text) function contains non-ASCII characters.

    Here is a breakdown of the error message:

    1. The core issue is a UnicodeEncodeError, which is raised because the 'ascii' codec cannot encode the character with the hex value \x9c (which is not a valid ASCII character).

    2. While handling this UnicodeEncodeError, a ValueError was then raised inside your process_qrcode_data function.

    To handle this error, you need to ensure that the input string to b64decode is ASCII-encoded. Here are some steps you can take to address the issue:

    • Confirm that the input text to base64.b64decode is indeed a base64-encoded string. Base64-encoded strings should only contain ASCII characters, so if you are getting non-ASCII characters, there might be an issue with how the text was encoded or retrieved in the first place.
    • Ensure that text does not contain any non-base64/ASCII characters before attempting to decode it. You can do this by filtering out any non-ASCII characters or identifying why non-ASCII characters are appearing in a base64 string.

    Here's a quick example of how you might clean the input text to remove non-ASCII characters before decoding:

    import base64
    
    def safe_b64decode(input_str):
        # Encode the input string to bytes, replace non-ASCII bytes with the standard base64 padding character '='
        ascii_str = input_str.encode('ascii', 'ignore').decode('ascii')
        # Proceed to base64 decode the ASCII string
        return base64.b64decode(ascii_str)
    
    try:
        # Example base64 string with a non-ASCII character '\x9c'
        text = "YourBase64StringHere\x9c"
        compressed_data = safe_b64decode(text)
        # ... rest of your code ...
    except ValueError as e:
        print(f"Error: {e}")
    

    If you find that the text is being encoded into base64 incorrectly, you'll need to fix the encoding process. Once you have a valid base64-encoded ASCII string, base64.b64decode should work without errors.

    Keep in mind this is a simple fix and you might need to adjust your code to handle the specific circumstances of how your text data is being generated. Additionally, using ignore during the encoding might remove information that is vital for the decoding process, so the above snippet might not be suitable for all use cases.