# File encoding detection library

**URL:** <https://forum.elixirforum.com/t/file-encoding-detection-library/34972>\
**Category:** Questions\
**Tags:** csv\
**Created:** [October 16, 2020, 7:53am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972 "2020-10-16T07:53:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![patryk.it](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/patryk.it/32/20513_2.png) [@patryk.it](https://forum.elixirforum.com/u/patryk.it)\
**Post date:** [October 16, 2020, 7:53am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/1 "2020-10-16T07:53:31Z")

</div>

Hi guys!

My question is related to the file encoding topic. I’m working on a feature that allows users to upload a CSV file that might be in different charset. It’s not only about UTF-8 and I need to detect given encoding and then parse data.

In Golang, Python, Java, C++, Objective-C we have UniversalDetector/chardet libraries. Do we have something similar to the Elixir/Erlang usage? I’m stuck with this.

How are you working with the different charsets/encodings in Elixir applications? I know - that’s not the main problem to which was designed this language, but I won’t use erlports or AWS Lambda only for detecting charset. ☹

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [October 16, 2020, 7:59am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/2 "2020-10-16T07:59:00Z")

</div>

> [@patryk.it](#):
>
> and then parse data.

nimble\_csv supports converting data to utf-8 before parsing / converting from utf-8 before dumping. By default it even ships `NimbleCSV.Spreadsheet`, which uses utf-16 le to work with excel csv’s. But you’ll need to setup a module per encoding.

---

<div class="post-metadata">

**Author:** ![patryk.it](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/patryk.it/32/20513_2.png) [@patryk.it](https://forum.elixirforum.com/u/patryk.it)\
**Post date:** [October 16, 2020, 9:06am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/3 "2020-10-16T09:06:37Z")

</div>

Thanks for your reply.  
You’re right - NimbleCSV supports encoding converting (via `:encoding` option), but my problem is that I don’t know the encoding of the incoming file. It may be UTF-8 or many different, like these with German characters.

When I’m trying to parse a CSV with German characters I got

```elixir
no function clause matching in CSV.Decoding.Preprocessing.Lines.starts_sequence?/5

```

Meh. Do I need to port `chardet` from the other languages? I’m stuck with this problem.

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [October 16, 2020, 9:12am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/4 "2020-10-16T09:12:31Z")

</div>

I know that’s why I explicitly only quoted you asking for the parsing part.

Generally I’d suggest you trying utf-16 le. If the csv comes out of excel this will be the encoding. If you really need the dark arts of detecting encoding (especially if no byte order mark is set) then you’ll need to wait for the input of people more knowledgeable in that than myself.

I’ve by now just let people copy and paste out of excel into a textarea, which results in csv format getting pasted as well.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [October 16, 2020, 9:23am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/5 "2020-10-16T09:23:02Z")

</div>

There is no way to get what you want.

Ask your clients to upload using a specified charset _only_.

If you see the single byte `0xC4`, that _could_ be a latin-1 encoded `Ä`, though in ISO-8859-5 (kyrillic) it would be as valid as with latin-1 though encoding a different character, the `Ф`.

Therefore it is _impossible_ to detect the encoding without knowing the content in advance.

What you describe from the other languages, is usually a very dumb heuristic.

1. Check if the input is valid UTF-8/16/32 by corresponding validators, perhaps even helped by a BOM
2. If not, fall back to a single byte encoding derived from the hosts local settings

---

<div class="post-metadata">

**Author:** ![patryk.it](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/patryk.it/32/20513_2.png) [@patryk.it](https://forum.elixirforum.com/u/patryk.it)\
**Post date:** [October 16, 2020, 9:23am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/6 "2020-10-16T09:23:08Z")

</div>

Anyway thanks a lot for trying to help! Yes, I don’t have BOM set in these files.

I’m waiting for encoding hero. 😃

---

<div class="post-metadata">

**Author:** ![harmon25](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/harmon25/32/1274_2.png) [@harmon25](https://forum.elixirforum.com/u/harmon25)\
**Post date:** [October 16, 2020, 10:46pm UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/7 "2020-10-16T22:46:58Z")

</div>

Tried out a package today called ExMagic that is a nif wrapper around ‘libmagic’… If running the ‘file’ Unix command helps determine the text encoding (not sure if it does) you might get something meaningful from ExMagic by passing it some raw binary… could get away with just the first 2kb or so of the file

---

<div class="post-metadata">

**Author:** ![ondrej-tucek](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ondrej-tucek/32/11566_2.png) [@ondrej-tucek](https://forum.elixirforum.com/u/ondrej-tucek)\
**Post date:** [October 17, 2020, 1:33pm UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/8 "2020-10-17T13:33:22Z")

</div>

I think that the `file` cmd does, see lesmana’s [reply](https://unix.stackexchange.com/questions/11602/how-can-i-test-the-encoding-of-a-text-file-is-it-valid-and-what-is-it). In short

```elixir
$ file -i *
umlaut-iso88591.txt: text/plain; charset=iso-8859-1
umlaut-utf16.txt: text/plain; charset=utf-16le
umlaut-utf8.txt: text/plain; charset=utf-

```

There are also another unix cmds: [enca](https://linux.die.net/man/1/enca) or [uchardet](https://www.freedesktop.org/wiki/Software/uchardet/). So connect them via ports…

---

<div class="post-metadata">

**Author:** ![harmon25](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/harmon25/32/1274_2.png) [@harmon25](https://forum.elixirforum.com/u/harmon25)\
**Post date:** [October 17, 2020, 4:25pm UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/9 "2020-10-17T16:25:26Z")

</div>

Nice!  
Not sure if the ExMagic package is running the code with the equivalent option to `-i` to actually get back the encoding - think it just returns the mime… might be an easy tweak though.

---

<div class="post-metadata">

**Author:** ![evadne](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/evadne/32/13949_2.png) [@evadne](https://forum.elixirforum.com/u/evadne)\
**Post date:** [October 17, 2020, 10:52pm UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/10 "2020-10-17T22:52:10Z")

</div>

The libMagic route is definitely worth considering. I have a library for that. Low profile, but working.

[https://github.com/evadne/gen\_magic](https://github.com/evadne/gen_magic)

---

<div class="post-metadata">

**Author:** ![harmon25](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/harmon25/32/1274_2.png) [@harmon25](https://forum.elixirforum.com/u/harmon25)\
**Post date:** [October 18, 2020, 3:40am UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/11 "2020-10-18T03:40:46Z")

</div>

I ended up using [ExMagic](https://github.com/liamwhite/exmagic) as it is just a Niff wrapper without any processes/gen\_server.

I am curious though, you mention this:

> The Server should be run under a pool which provides concurrency _and_ resiliency

Is that really necessary? I may not fully understand Niffs - does calling a niff function get serialized somehow? I figured if you were calling the niff from say multiple beam processes they would all just invoke the C code independently of each other, and not require being wrapped in a pool for concurrent access?

---

<div class="post-metadata">

**Author:** ![evadne](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/evadne/32/13949_2.png) [@evadne](https://forum.elixirforum.com/u/evadne)\
**Post date:** [October 18, 2020, 1:18pm UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/12 "2020-10-18T13:18:44Z")

</div>

Pools as in Poolboy so you have the benefit of not touching the content of the file within the OS process responsible for running BEAM — design wise, I like to isolate external programs and communicate with them using ports rather than use NIFs. When a NIF crashes the whole thing comes down.

---

<div class="post-metadata">

**Author:** ![harmon25](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/harmon25/32/1274_2.png) [@harmon25](https://forum.elixirforum.com/u/harmon25)\
**Post date:** [October 18, 2020, 2:47pm UTC](https://forum.elixirforum.com/t/file-encoding-detection-library/34972/13 "2020-10-18T14:47:48Z")

</div>

> [@evadne](#):
>
> When a NIF crashes the whole thing comes down.

While in many cases I would agree. I think libmagic and the file(1) command in unix has been tested extremely thoroughly and if it were to crash with some arbitrary input - that would be worthy of a CVE.

If you write your own low level c implementation and do not have google security engineers fuzzing it for weeks - then yea, would probably be safer as a port…

When comparing gen\_magic to ExMagic, I figured the less code wrapped around libmagic - the better.

IMO the addition of a pool, and state(gen\_statem) to libmagic is unnecessary complexity - that may introduce different bugs…
