Search code examples
unicodeerlangelixirlatin1

no_translation error while attempting to write unicode characters to file in Elixir


I have a stream that I'm reading from and in turn am writing to a file however I am getting an error that is caused by the presence of a . I assume this is because I'm opening the file w/ the wrong encoding or something but I don't know how to properly set it:

file = File.open!("/some/path.csv", [:write])
IO.write(file, "’")

This results in the following error:

** (ErlangError) erlang error: :no_translation (stdlib) :io.put_chars(#PID<0.250.0>, :unicode, "’")


Solution

  • You should open the file in :utf8 mode.

    file = File.open!("/tmp/foo.txt", [:write, :utf8])
    IO.write(file, "’")