# Replace non ascii char

**URL:** https://discourse.julialang.org/t/replace-non-ascii-char/64358
**Category:** General Usage
**Created:** [July 9, 2021, 2:54pm UTC](https://discourse.julialang.org/t/replace-non-ascii-char/64358 "2021-07-09T14:54:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![agatheLG](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@agatheLG](https://discourse.julialang.org/u/agatheLG)
#### Post date: [July 9, 2021, 2:54pm UTC](https://discourse.julialang.org/t/replace-non-ascii-char/64358/1 "2021-07-09T14:54:19Z")

</div>

Hi,  
I am switching a lot between format from dataframe to csv file to sql and of course I have an error about invalid character (Base.InvalidCharError{Char}(‘\x8e’) when I try to load my data into SQL.  
I can indeed see that there is non ascii char in my file “n\x8eomutation” when I print it in my julia REPL, which shows as an interrogation mark in a square in my terminal.  
I have tried loads of different technique such as `replace(mystring, "[^\\x00-\\x7F]" => " ")` or `ascii(mystring)` or `unidecode(mystring)`… Ascii find the index of the non ascii character but throw an error and doesn’t offer the option to return this index or directly change this char. I have also tried operating the change on the dataframe and on th csv file but neither work. Do you have any idea with julia how I could remove or replace non ascii character from either a string, a file or a dataframe ?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 9, 2021, 3:52pm UTC](https://discourse.julialang.org/t/replace-non-ascii-char/64358/2 "2021-07-09T15:52:55Z")

</div>

> [@agatheLG](#):
>
> I am switching a lot between format from dataframe to csv file to sql and of course I have an error about invalid character (Base.InvalidCharError{Char}(’\x8e’) when I try to load my data into SQL.  
> I can indeed see that there is non ascii char in my file “n\x8eomutation” when I print it in my julia REPL, which shows as an interrogation mark in a square in my terminal.

Ideally, you would figure out _why_ you have these characters in your data and fix the problem at the source, rather than just discarding non-ascii bytes.

Julia handles non-ascii Unicode characters just fine as _long_ as your text is encoded in the UTF-8 encoding. Maybe you just need to tell your data source to use UTF-8? Alternatively, you can use the [StringEncodings.jl package](https://github.com/JuliaStrings/StringEncodings.jl) to convert from data in a different encoding.

That being said, if you want to discard non-ascii characters, that is easy to do. e.g. `filter(isascii, mystring)` works. (_Update_: I’ve added a [note to the `ascii(s)` docs](https://github.com/JuliaLang/julia/pull/41527) about this.) To instead replace non-ascii characters with spaces, you could do `replace(mystring, !isascii=>' ')`.

---

<div class="post-metadata">

### Author: ![agatheLG](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@agatheLG](https://discourse.julialang.org/u/agatheLG)
#### Post date: [July 9, 2021, 4:44pm UTC](https://discourse.julialang.org/t/replace-non-ascii-char/64358/3 "2021-07-09T16:44:59Z")

</div>

Great thank you so much `replace(mystring, !isascii=>' ')` is exactly what I was looking for but I couldn’t manage to find the appropriate syntaxe… And I completly agree, it would make so much more sense to fix the problem at the source however I don’t have a hold on the file generated at the source. Thank you again
