How to write Arabic, Hebrew Into CSV file?

Roy Doron picture Roy Doron · Dec 16, 2012 · Viewed 7.6k times · Source

I can't write to Results.csv any of the languages Arabic or Hebrew, except English. Each time I'm trying to write any one of them I'm getting gibberish marks, in the CSV file where it should have proper Arabic or Hebrew words instead.

I've been trying to use UTF8Encoding, but I'm obviously doing something wrong.

Please take a look and try to correct me:

private void WriteToCsvFile()
{
    var us = users.ElementAt(0);
    string titlenames = "number,";
    string userAnswer = (us.userName + ",");
    foreach (string ss in List)
    {
        // Do stuff
    }
    foreach (string ans in us.answer)
    {
        // Do stuff
    }
    using (StreamWriter sw = new StreamWriter("Results.csv", true, Encoding.UTF8))
    {
        sw.WriteLine(titlenames);
        sw.WriteLine(userAnswer);
    }
    this.Close();
}

Thank you for your time and help!

Here is the CSV file with different kinds of Encoding:

number  לוי  סל    רמי  שופר    ×ב×לה    ×‘×•×   ללונה  פ×רק
22  FALSE   TRUE    FALSE   FALSE   TRUE    TRUE    FALSE   FALSE

number  לוי  סל    רמי  שופר    ×ב×לה    ×‘×•×   ללונה  פ×רק
33  TRUE    TRUE    FALSE   FALSE   FALSE   FALSE   TRUE    FALSE

number  +BdwF1QXZ-  +BeEF3A-    +BegF3gXZ-  +BekF1QXkBeg-   +BdAF0QXQBdwF1A-    +BdEF1QXQ-  +BdwF3AXVBeAF1A-    +BeQF0AXoBec-
22  FALSE   FALSE   FALSE   TRUE    TRUE    TRUE    FALSE   TRUE

number  ÜÕÙ áÜ  èÞÙ éÕäè    ÐÑÐÜÔ   ÑÕÐ ÜÜÕàÔ   äÐèç
33  TRUE    TRUE    FALSE   TRUE    FALSE   TRUE    TRUE    FALSE

number  ÜÕÙ áÜ  èÞÙ éÕäè    ÐÑÐÜÔ   ÑÕÐ ÜÜÕàÔ   äÐèç
44  not answered    not answered    FALSE   not answered    not answered    not answered    not answered    not answered

number  ??? ??  ??? ????    ?????   ??? ?????   ????
55  not answered    not answered    not answered    not answered    not answered    not answered    not answered    not answered

As you can see, gibbrish marks should be names in Arabic and Hebrew.

Answer

Abel picture Abel · Dec 16, 2012

You say you are getting "gibberish marks" in your CSV file. That is not because you are writing using the wrong encoding, but that is (most likely) because you are viewing it using the wrong encoding.

Try opening the CSV file using a text editor like Notepad+. Select in the Encoding menu UTF8. You should be seeing the correct characters now. If not, please attach an example CSV file you created with the above code, maybe I can see what goes wrong.