Text file to CSV file (x by 2)

Hi all

I can’t work out how to pull data out of a .txt file and put it into a .csv file (2 columns and x rows). I’ve made a simplified version below.

The length of the txt file is dynamic but the output will always be 2 columns

I thought that I should build a datatable and then feed the data into it but I don’t seem to manage, no matter what I try. Any help would be really appreciated. Thanks very much.

Mock-up .txt file

Bob
US
Jack
UK
John
UK

Required Output (.csv)
I can’t display the below as a table in this post but obviously I need two columns of data

Column 1
Bob
Jack
John

Column 2
US
UK
UK

Using Linq something like that without headers, but I think you can figure out how to add header

3 Likes

@Uemoe - thank you very much! It worked perfectly.

Although, I don’t understand that formula. I will see if I can work it out.

Thanks again

@Uemoe - how can I expand that expression to handle add a third column if there was a third data point? “Zip” appears to only handle two inputs.

Thanks for your help

image

It’s actually POC. It’s not very effective and far less maintainable.

Here is another way for doing batching in Linq without MoreLinq (or writing own extension method)

image

Not very effective either but more scalable

If you aren’t familiar with Linq, probably better approach would be return to basics: write some simple loop on array of strings and carefully deal with tail of your sequence.

1 Like

Thanks for your explanation @Uemoe.

I’m not familiar with Linq but am interested in learning more. I’ll also see if I can do a non-Linq version, as you mentioned.

Thanks again.