Home > OS >  C# Linq doesn't recognize Czech characters while reading from .csv file
C# Linq doesn't recognize Czech characters while reading from .csv file

Time:08-02

Basically, when trying to get a .csv file into a list using Linq, all characters with diacritics turn into <?> character. What should i do to make the code keep them as in the .csv file?

using (StreamReader ctec = new StreamReader(souborovejmeno))
            {
                var lines = File.ReadAllLines(souborovejmeno).Select(a => a.Split('\t'));
                var csv = from line in lines
                          select (from piece in line
                          select piece).ToList();

                foreach (var c in csv)
                {
                    hraci.Add(new Hrac(c[0], c[1]));
                    listBox1.Items.Add(c[0]);
                }
            }

Thanks in advance for answers. Sorry if this is quite dumb, i am not too experienced in coding.

CodePudding user response:

I think your problem is missed encoding. I see you already have answer above that works.

var lines = File.ReadAllLines(path, Encoding.UTF8).Select(a => a.Split('\t'));

But I strongly recommend you to use CsvHelper

dotnet add package CsvHelper 

And use something like this

public class Record
{
   [Index(0)]
   public int Key { get; set; }

   [Index(1)]
   public string Value { get; set; }
}
....
using (var reader = new StreamReader(souborovejmeno, Encoding.UTF8))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    var records = csv.GetRecords<Record>();
    foreach(var record in records) {
        hraci.Add(new Hrac(record.Key, record.Value));
        listBox1.Items.Add(record.Key);
    } 
}
...

CodePudding user response:

Try to include an encoding like that:

var lines = File.ReadAllLines(path, Encoding.UTF8).Select(a => a.Split('\t'));

Make sure to import System.Text

  • Related