jayt.dev
jayt.dev

Reputation: 1015

Reading a CSV file containing greek characters

I am trying to read the data from a CSV file using the following:

var lines = File.ReadAllLines(@"c:\test.csv").Select(a => a.Split(';'));

It works but the fields that contain words are written with Greek charactes and they are presented as symbols. How can I set the Encoding correctly in order to read those greek characters?

Upvotes: 2

Views: 4632

Answers (1)

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

ReadAllLines has overload, which takes Encoding along file path

var lines = File.ReadAllLines(@"c:\test.csv", Encoding.Unicode)
                .Select(line => line.Split(';'));    

Testing:

File.WriteAllText(@"c:\test.csv", "ϗϡϢϣϤ", Encoding.Unicode);

Console.WriteLine(File.ReadAllLines(@"c:\test.csv", Encoding.Unicode));

will print:

ϗϡϢϣϤ

To find out in which encoding the file was actually written, use next snippet:

using (var r = new StreamReader(@"c:\test.csv", detectEncodingFromByteOrderMarks: true)) 
{
    Console.WriteLine (r.CurrentEncoding.BodyName);
}

for my scenario it will print

utf-8

Upvotes: 1

Related Questions