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
|
Show 2 more comments
1 Answer
Reset to default 1A 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
CsvReader
andCsvWriter
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