Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to write a program which changes the column order of a CSV file.
Im using the following code

VB.NET
Public Shared Sub ReadCSV()

        Dim columnstoselect As String()

        Dim csvFile As String = "D:\Projects\MQA_Remap_CSV\Input.csv"
        Dim lines = File.ReadAllLines(csvFile)

        Dim outFile As String = "D:\Projects\MQA_Remap_CSV\Output.csv"
        Dim data As String()

        Dim format As String = CReadConfig.ReadConfigSource("D:\Projects\MQA_Remap_CSV\config.csv")

        Form1.TextBox1.Text = CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToString

Using fs As New StreamWriter(outFile, False)
            For Each s As String In lines

                data = s.Split(","c)

                fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray
                                         )
                            )
            Next
        End Using


I am having a problem with this function

VB
CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv")


the function looks like this

VB.NET
Public Shared Function ReadConfigtarget(filePath As String) As List(Of String)
    'returns the Value of target or array rows as an array
    Dim str As String()
    Dim lines = File.ReadAllLines(filePath).Skip(1)
    Dim val As String
    Dim i As String = 0

    Using sr As New StreamReader(filePath)

        Dim out As New List(Of String)

        For Each s As String In lines
            str = s.Split(","c)
            val = Trim("data(" & (str(1) - 1) & ") ").ToString
            out.Add(val)
        Next

        Return out

    End Using
End Function


this function reads a csv file and should return a list of values which look like this
data(0), data(1)

so in effect if i replace this line
C#
fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray


with

C#
fs.WriteLine(String.Format(format, data(0), data(1), data(2))


the code works and writes out the data in data(0) ,data(1) and so on , however when i use the function it is taking it as a literal string and writing out text 'data(0)' , data(1) in the csv file.

Any ideas on how to pass the function values as argument and not literal text?
Posted
Updated 1-Feb-16 20:08pm
v2
Comments
CPallini 2-Feb-16 2:54am    
What problem do you have with
fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray()))
?
It should work, actually.

1 solution

If it's writing the variable names as the output then what you've probably done is:
VB
fs.WriteLine(String.Format(format, "data(0), data(1), data(2)"))
Or
VB
fs.WriteLine(String.Format("{0},{1},{2}, data(0), data(1), data(2)"))

Instead.
But Carlo is right:
VB
fs.WriteLine(String.Format(format, CReadConfig.ReadConfigtarget("D:\Projects\MQA_Remap_CSV\config.csv").ToArray()))
will work perfectly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900