1

enter image description hereI have a CSV files output from a software without headers, I need to change the order of columns based on a config file

initial-column Final-Column 1 5 2 3 3 1

Any ideas how to go about this?

Viladimir
  • 137
  • 1
  • 2
  • 9

1 Answers1

2

There is very very little to go on, such as how the config file works and what the data looks like.

Note that using the layout of {1, 5, 2, 3, 3, 1} you arent just reordering the columns, that drops one (4) and duplicates columns 1 and 3.

Using some fake random data left over from this answer, this reads it in, then writes it back out in a a different order. You will have to modify it to take the config file into consideration.

Sample data:

Ndxn fegy n, 105, Imaypfrtzghkh, -1, red, 1501
Mfyze, 1301, Kob dlfqcqtkoccxwbd, 0, blue, 704
Xe fnzeifvpha, 328, Mnarhrlselxhcyby hq, -1, red, 1903

Dim csvFile As String = "C:\Temp\mysqlbatch.csv"
Dim lines = File.ReadAllLines(csvFile)

Dim outFile As String = "C:\Temp\mysqlbatch2.csv"
Dim data As String()

Dim format As String = "{0}, {4}, {1}, {2}, {2}, {0}"

Using fs As New StreamWriter(outFile, False)
    For Each s As String In lines
        ' not the best way to split a CSV,
        ' no OP data to know if it will work
        data = s.Split(","c)
        ' specifiy the columns to write in
        ' the order desired
        fs.WriteLine(String.Format(format,
                                   data(0),
                                   data(1),
                                   data(2),
                                   data(3),
                                   data(4),
                                   data(5)
                                 )
                    )
    Next
End Using

This approach uses the format string and placeholder ({N}) to control the order. The placeholders and array elements are all zero based, so {1, 5, 2, 3, 3, 1} becomes {0, 4, 1, 2, 2, 0}. Your config file contents could simply be a collection of these format strings. Note that you can have more args to String.Format() than there are placeholders but not fewer.

Output:

Ndxn fegy n, red, 105, Imaypfrtzghkh, Imaypfrtzghkh, Ndxn fegy n
Mfyze, blue, 1301, Kob dlfqcqtkoccxwbd, Kob dlfqcqtkoccxwbd, Mfyze
Xe fnzeifvpha, red, 328, Mnarhrlselxhcyby hq, Mnarhrlselxhcyby hq, Xe fnzeifvpha

Splitting the incoming data on the comma (s.Split(","c)) will work in many cases, but not all. If the data contains commas (as in some currencies "1,23") it will fail. In this case the seperator char is usually ";" instead, but the data can have commons for other reasons ("Jan 22, 2016" or "garden hose, green"). The data may have to be split differently.

Note: All the OPs previous posts are vba related. The title includes VB.NET and is tagged , so this is a VB.NET answer

Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Thank you for you help and patience ,I'm just starting out with coding.! – Viladimir Jan 21 '16 at 03:55
  • I wrote a function to read the text file and generate a string `code fs.WriteLine(String.Format(format, CReadConfig.ReadConfigsource(ConfigFilepath))) ` the function returns this value data(0),data(1),data(2),data(3) the format string is : {0},{1},{2},{3} I'm getting an error: Index(Zero based) must be greater than or equal to Zero and less than the size of the argument list. – Viladimir Jan 21 '16 at 06:30
  • I have no idea exactly what is in the config file or how you processed it. You *certainly* do not need to call the function in the loop. `{0},{1},{2},{3}` looks legal, but it wont reorder the columns - note how that the placeholders are what control the output order. You should ask a new question so you can show the config file contents and the method you have to read it and work out the order. – Ňɏssa Pøngjǣrdenlarp Jan 21 '16 at 14:33