如何在 C# 中正确读取 CSV 文件中的捷克语字符?

问题描述 投票:0回答:1

对于我的 C# WPF 应用程序,我需要导入 CSV 文件(用 ; 分隔)。 CSV 文件中有许多不同的列(其中一些甚至是空的)。第一列和第二列包含由不同字符组成的名字和姓氏 - 其中一些来自捷克字母(如 ščřěžýáíé 或 ŠČŘĚŽÝÁÍÉ)。

我需要在我的应用程序中正确解析这个名字和姓氏。

你能帮我吗?

这是 CSV 文件的示例:

"name";"surname" "Adam";"Veselý" "David";"Řehořčík"

我尝试了很多不同的方法。也许有一个图书馆可以做到这一点?我已经尝试过 CsvHelper 包来解析 CSV 文件,但我无法让它工作 - 而不是它放置的变音符号字符 � 字符。我尝试使用“Encoding("UTF8")”将编码更改为 UTF8,但没有任何改变。

这是我尝试使用的函数的最终版本:

public void ImportStudentsFromCsv(object obj)
{
    // Open file dialog to select CSV file
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "CSV files (*.csv)|*.csv";
    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    openFileDialog.Title = "Select CSV file";

    if (openFileDialog.ShowDialog() == true)
    {
        string filePath = openFileDialog.FileName;

        try
        {
            // Configure CsvHelper to handle semicolon delimiter and diacritics
            var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                Delimiter = ";",
                HasHeaderRecord = true, // Assume CSV has header
                Encoding = Encoding.UTF8,
                BadDataFound = null // Ignore bad data
            };

            using (var reader = new StreamReader(filePath, Encoding.UTF8))
            using (var csv = new CsvReader(reader, csvConfig))
            {
                // Read CSV records into Student objects
                var records = csv.GetRecords<dynamic>();

                // Initialize the students list
                _students = new List<Student>();

                foreach (var record in records)
                {
                    // Map CSV values to Student properties
                    var student = new Student
                    {
                        StudentName = record.jmeno,
                        StudentSurname = record.prijmeni,
                        StudentProgram = record.oborKomb,
                        StudentYear = Convert.ToInt32(record.rocnik),
                        ClassId = SelectedClass.Id,
                        Card = "card not assigned"
                    };

                    // Add student to the list
                    _students.Add(student);
                }
            }

            // Save students to the database
            using (var db = new UserContext())
            {
                db.student.AddRange(_students);
                db.SaveChanges();
            }

            // Update the UI
            OnPropertyChanged(nameof(Students));
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Error occurred while importing students: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}

c# .net csv encoding
1个回答
0
投票

编码.UTF8

您的文件可能不是

UTF-8
。也许是
Windows-1250
。没有看到文件,恐怕无从得知。我只是尝试使用
Encoding.GetEncoding("Windows-1250")
而不是 UTF-8,看看它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.