Last active
April 17, 2025 16:27
-
-
Save germ13/9de46a78e4bae9603656926eb4a4cb2b to your computer and use it in GitHub Desktop.
map one csv to another csv rearranging headers and renaming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System | |
Imports System.IO | |
Module Program | |
Sub Main(args As String()) | |
' Read the input file | |
Dim inputFilePath As String = "testResults.txt" | |
Dim inputLine As String = File.ReadAllText(inputFilePath).Trim() | |
' Parse the input line into a list of entries, removing double quotes | |
Dim entries As List(Of String) = inputLine.Split(","c).Select(Function(s) s.Trim().Replace("""", "")).ToList() | |
' Define the mapping with associated headers and a flag for quoting | |
Dim mapping As New Dictionary(Of Integer, (Integer, String, Boolean)) From { | |
{0, (2, "Header3", False)}, ' "apple" -> "cherry" with "Header3" (no quotes) | |
{1, (0, "Header1", True)}, ' "banana" -> "apple" with "Header1" (quoted) | |
{2, (4, "Header5", False)}, ' "cherry" -> "elderberry" with "Header5" (no quotes) | |
{3, (1, "Header2", True)}, ' "date" -> "banana" with "Header2" (quoted) | |
{4, (3, "Header4", False)}, ' "elderberry" -> "date" with "Header4" (no quotes) | |
{5, (5, "Header6", False)}, ' "fig" -> "fig" with "Header6" (no quotes) | |
{6, (7, "Header8", True)}, ' "grape" -> "honeydew" with "Header8" (quoted) | |
{7, (6, "Header7", False)} ' "honeydew" -> "grape" with "Header7" (no quotes) | |
} | |
' Rearrange the entries and headers based on the mapping | |
Dim rearrangedHeaders As New List(Of String) | |
Dim rearrangedEntries As New List(Of String) | |
For i As Integer = 0 To mapping.Count - 1 | |
' Add the header, quoting if necessary | |
Dim header = mapping(i).Item2 | |
If mapping(i).Item3 Then | |
header = $"""{header}""" | |
End If | |
rearrangedHeaders.Add(header) | |
' Add the corresponding entry | |
rearrangedEntries.Add(entries(mapping(i).Item1)) | |
Next | |
' Combine headers and rearranged entries | |
Dim outputLines As New List(Of String) | |
outputLines.Add(String.Join(",", rearrangedHeaders)) | |
outputLines.Add(String.Join(",", rearrangedEntries)) | |
' Write the output to a file | |
Dim outputFilePath As String = "rearrangedResults.txt" | |
File.WriteAllText(outputFilePath, String.Join(Environment.NewLine, outputLines)) | |
Console.WriteLine("Rearranged results with headers written to " & outputFilePath) | |
' Wait for user input before closing the console | |
Console.WriteLine("Press any key to exit...") | |
Console.ReadKey() | |
End Sub | |
End Module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment