最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

utf 8 - Convert file csv with UTF-8 format to ANSI in C# - Stack Overflow

programmeradmin2浏览0评论

I have .CSV files with UTF-8 encoding, and I want to convert these files to ANSI format, but my code doesn't work!

static void Main()
{
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

    string inputFile = "C:\\20250201.csv"; // Path to the UTF-8 CSV file
    string outputFile = "C:\\20250201.csv"; // Path to the ANSI CSV file

    // Read UTF-8 CSV
    using (var reader = new StreamReader(inputFile, System.Text.Encoding.UTF8))
    using (var csvReader = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)))
    {
        var records = csvReader.GetRecords<dynamic>(); // Read records dynamically

        // Write ANSI CSV
        using (var writer = new StreamWriter(outputFile, false, System.Text.Encoding.GetEncoding(1252))) // ANSI (Windows-1252)
        using (var csvWriter = new CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture)))
        {
            csvWriter.WriteRecords(records); // Write all records
        }
    }

    Console.WriteLine("Conversion completed: UTF-8 to ANSI");
}

I have .CSV files with UTF-8 encoding, and I want to convert these files to ANSI format, but my code doesn't work!

static void Main()
{
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

    string inputFile = "C:\\20250201.csv"; // Path to the UTF-8 CSV file
    string outputFile = "C:\\20250201.csv"; // Path to the ANSI CSV file

    // Read UTF-8 CSV
    using (var reader = new StreamReader(inputFile, System.Text.Encoding.UTF8))
    using (var csvReader = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)))
    {
        var records = csvReader.GetRecords<dynamic>(); // Read records dynamically

        // Write ANSI CSV
        using (var writer = new StreamWriter(outputFile, false, System.Text.Encoding.GetEncoding(1252))) // ANSI (Windows-1252)
        using (var csvWriter = new CsvWriter(writer, new CsvConfiguration(CultureInfo.InvariantCulture)))
        {
            csvWriter.WriteRecords(records); // Write all records
        }
    }

    Console.WriteLine("Conversion completed: UTF-8 to ANSI");
}
Share Improve this question edited Feb 7 at 19:52 Thomas Dickey 54.5k8 gold badges78 silver badges108 bronze badges asked Feb 7 at 16:51 sarasara 11 bronze badge 7
  • 7 Please clarify what you mean by "doesn't work". – Michael Liu Commented Feb 7 at 16:55
  • 3 CsvReader and CsvWriter are not needed, converting encoding of CSV file here is just an equivalent of converting a text file from UTF8 to ANSI – Renat Commented Feb 7 at 17:25
  • 1 Keep UTF-8 Everywhere… – JosefZ Commented Feb 7 at 19:50
  • UTF-8 is not “UTF-8 format.” And why would you need ASCII, ever? – Sergey A Kryukov Commented Feb 8 at 1:18
  • The application runs without errors and completes the process successfully. However, when I check the output file, I notice that the encoding is still UTF-8. Additionally, I have identified a potential issue with the CSV structure: The first few rows contain 2 or 3 values separated by commas. The remaining rows contain more values separated by commas. – sara Commented yesterday
 |  Show 2 more comments

1 Answer 1

Reset to default 1

A comma separated values (.csv) file is simply a text file that contains text values that are separated by commas.

In the sample CSV file below I've included smart-quotes.

Test.csv (UTF-8):

Name, Address, City, StateOrProvince
“John Doe”, 123 Walnut Street, Venice, FL
“Bob Smith”, 456 State Street, Naples, FL

Note: To create the sample CSV file on your computer, open Notepad. Copy and paste the text to Notepad. Click Save As. For Encoding select either UTF-8 with BOM or UTF-8. Click Save.


Add using directive:

  • using System.IO;

Option 1:

private void ConvertFromUtf8ToAnsi(string sourceFilename, string outputFilename)
{
    using (StreamReader sr = new StreamReader(sourceFilename, true))
    {
        //using (StreamWriter sw = new StreamWriter(outputFilename, false, System.Text.Encoding.GetEncoding(1252)))
        using (StreamWriter sw = new StreamWriter(outputFilename, false, System.Text.Encoding.Default))
        {
            while (!sr.EndOfStream)
            {
                sw.WriteLine(sr.ReadLine());
            }
        }
    }
}

Option 2:

private void ConvertFromUtf8ToAnsi(string sourceFilename, string outputFilename)
{

    using (FileStream fsInput = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read))
    {
        using (StreamReader sr = new StreamReader(fsInput, true))
        {
            using (FileStream fsOutput = new FileStream(outputFilename, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fsOutput, System.Text.Encoding.Default))
                //using (StreamWriter sw = new StreamWriter(outputFilename, false, System.Text.Encoding.GetEncoding(1252)))
                {
                    while (!sr.EndOfStream)
                    {
                        sw.WriteLine(sr.ReadLine());
                    }
                }
            }
        }
    }

}

Option 3:

private void ConvertFromUtf8ToAnsi(string sourceFilename, string outputFilename)
{

    string[] data = System.IO.File.ReadAllLines(sourceFilename);
    //System.IO.File.WriteAllLines(outputFilename, data, Encoding.GetEncoding(1252));
    System.IO.File.WriteAllLines(outputFilename, data, Encoding.Default);

}

To check the encoding of the output file, open the output file using Notepad. Then click File => Save As. Look at the value for Encoding.

Resources:

  • HTML Unicode (UTF-8) Reference
  • UTF-8 Latin1 Supplement
  • Character sets: Windows-1252
  • How to reliably guess the encoding between MacRoman, CP1252, Latin1, UTF-8, and ASCII
发布评论

评论列表(0)

  1. 暂无评论