1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Error: "'utf-8' codec can't decode byte 0xd6 in position 4: invalid continuation...

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am receiving my ".wav" file in a django view as an "InMemoryUploadedFile" then converting it to bytes:

    views.py

    import functions.bytes_to_wav_wave

    if voice_value:
    # convert to bytes
    file_content_bytes = voice_value.read()
    wav_sound = bytes_to_wav_in_memory(file_content_bytes,1,2,10)
    buffer = io.BytesIO(wav_sound)

    try:
    file_w = bytes_to_wav_wave(file_content_bytes,'output_wave.wav', 1, 2, 44100)
    except Exception as e:
    print(e)

    with open('output_wave.wav', 'r') as file:
    content = file.read()


    While the function "bytes_to_wav_wave" is:

    functions.py

    def bytes_to_wav_wave(audio_bytes, filename, nchannels, sampwidth, framerate):
    """
    Converts raw audio bytes to a WAV file using the wave module.

    Args:
    audio_bytes (bytes): The raw audio data.
    filename (str): The name of the output WAV file.
    nchannels (int): Number of audio channels (e.g., 1 for mono, 2 for stereo).
    sampwidth (int): Sample width in bytes (e.g., 2 for 16-bit audio).
    framerate (int): Frame rate or sample rate in Hz (e.g., 44100).
    """
    with wave.open(filename, 'wb') as wf:
    wf.setnchannels(nchannels)
    wf.setsampwidth(sampwidth)
    wf.setframerate(framerate)
    wf.writeframes(audio_bytes)


    # Example usage:
    # Assuming 'raw_audio_data' is your bytes object containing audio data
    # bytes_to_wav_wave(raw_audio_data, 'output_wave.wav', 1, 2, 44100)


    But once I finish the conversion and come to opening my file with:

    Views.py

    try:
    file_w = bytes_to_wav_wave(file_content_bytes,'output_wave.wav', 1, 2, 44100)
    except Exception as e:
    print(e)

    try:
    with open('output_wave.wav', 'r') as file:
    content = file.read()
    except Exception as e:
    print("in try")
    print(e)


    The "except" block catches the error:

    **'utf-8' codec can't decode byte 0xc6 in position 4: invalid continuation byte **

    Continue reading...

Compartilhe esta Página