r/csharp•
spectroll

[Help] Manipulating data from text file

Hello guys.

I'm trying to manipulate the data from a txt file generated by a third party program. Here is basically the content of the file: http://pastebin.com/raw/gg15s10e

I think I can make some use of the StreamReader, maybe pass each line to an array and then turn the data into a CSV file.

I'm thinking basically in something like this: get the line, if the characters 1 to 19 contains "Name." then get the characters 20 to 83 and store in the variable name. Then I make the "right side column" of the txt file using the same logic to get the code, move to next line and so on.

Is that possible? Thats the approach? Since I'm still learning C# (my 1st language) I don't know if this is the best way to do that. Can you guys point me C# methods I can use here? Thank you.

Posted Nov 30, 2016, 2:08 PM UTC · edited

36
tweq•

That looks like a rather annoying format to parse consistently. You could get about 90% of the way with a simple regex, but then there are a bunch of exceptions like the "STATE" field in the middle of nowhere and the ADR lines.

Hardcoding the positions of the values like you suggested would be a possible option, but it would also be error-prone to maintain and set up.

What happens to overlong values? Is there a possibility of multi-line values being entered?

3
spectrollOP•

No they can't be multi-lines.

1
nosoupforyou•

Well, you could pass each line to a method and have the method do a split on each string for a double space. Then you'd have the label...: value, and you could process those a lot easier, especially with a split on ":".

You'd have to check for "ADR 1:" and "ADR 2:" and etc to test which address set to use.

string[] separator = { "  " };
string[] arrline = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in arrline)
{
    string[] arrdata = item.Split(':');
}

Admittedly, split might be slower than some code, but if you're screenscraping something, the refresh on that might be the big delay anyway. And unless you'd doing ton of them in a row, the speed probably won't be noticeable.

2
jeffrey_f•

The nice thing is that the "record" is consistent.

------- is your new record until the next ---------

Then parse each line positionally and each record (line) will have specific info. The "info header" on each line will tell you what it is.

Line 1 after the -------- is name and Code,

Line 2 is ID, state, sex and type

Take it from there, count your line and watch for a record change -------

1

Whoever designed that format must really hate themselves and anyone expected to work with it.

1
Gambleon•
foreach(String line = File.ReadAllLines("C:\\Path\\File.txt))

    if(line.Substring(0, 19).Contains("Name."))
        some_variable.Add(line.Substring(20,  83 
0