0

What's the best practice reading a *.txt file and get specific regions of the text?

My *.txt files looks like:

[Product code]
MYPRODUCT-CODE123

[List price]
28.10

[Price]
20.30

[Weight]
10

[Quantity]
1

[Min quantity]
1

[Shipping freight]
N

[Free shipping]
N

[Product name]
My product name

Currently I'm reading the txt file like this:

        String[] allfiles = System.IO.Directory.GetFiles(_path, "*.txt", System.IO.SearchOption.AllDirectories);

        foreach (string filePath in allfiles) {


            using (StreamReader sr = File.OpenText(filePath))
            {
                string s = sr.ReadToEnd();

            }
        }

How I'd get the text near [Product code], and so on for other 'key terms' from my txt file

DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39
el.severo
  • 2,202
  • 6
  • 31
  • 62

4 Answers4

2

I would just use a Regular Expression with capture groups to grab pairs, then load them into a Dictionary:

var dict = Regex
               .Matches(str, @"\[([^\]]+)\]([^\[]+)")
               .Cast<Match>()
               .ToDictionary(match => match.Groups[1].ToString(), 
                             match => match.Groups[2].ToString().Trim());

//dict = { [Product Code, MYPRODUCT-CODE123], [List Price, 28.10], [Price, 20.30] ...}

I would strongly recommend you store your data in XML format if you're keeping it all in text files though. It'll save you this hassle later.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • I'm not trying to store something, I'm working on a small app that will create a *.csv file based on a folder structure. I'm definitively agree with you. – el.severo Dec 01 '14 at 20:09
1

So you have your string s. Let's take it from there.

Split on new line, put pairs into dictionary, get the item:

var lines = s.Split(
                new[] { Environment.NewLine }, 
                StringSplitOptions.RemoveEmptyEntries)
             .ToArray();

// pairing thanks to http://stackoverflow.com/questions/1624341/
var dictionary = lines.Where((x, i) => i < lines.Length)
                      .Select((x, i) => 
                          new KeyValuePair<string, string>(
                              x.Trim('[', ']'), // get rid of brackets
                              lines[i + 1]))
                      .ToDictionary(x => x.Key, x => x.Value);

var productCode = dictionary["Product code"];
dav_i
  • 27,509
  • 17
  • 104
  • 136
0
System.IO.StreamReader file = 
   new System.IO.StreamReader("Your txt file");

Dictionary<string, string> values = new Dictionary<string, string>();
string keyContainer = "";

while((line = file.ReadLine()) != null)
{
   if(line.Trim() == "")
      continue;

   if(values.Keys.Contains(line.Trin())
      continue;   

   if(line.StartsWith('[') && line.EndsWith("]")
   {
       keyContainer = line.Trim();
       values.Add(line.Trim(), "");
   }
   else
   {
       values[keyContainer] = line.Trim();    
   }
}

With this code you will have all the values of the file in Dictionary. They will look like this:

Key=[Quantity]; Value=1

If you want you can remove the brackets when you save the key in the dictionary.

mybirthname
  • 17,949
  • 3
  • 31
  • 55
0

Another way to do it:

    string[] lines = input.Replace(Environment.NewLine, "\n").Replace('\r', '\n').Split('\n');
    for (int q = 0; q < lines.Length; q++)
    {
        string line = lines[q];
        if (string.IsNullOrWhiteSpace(line))
            continue;

        if (line.StartsWith("[") && line.EndsWith("]"))
        {
            string key="";
            string value="";

            for (int i=1; i<line.Length - 1; i++)
            {
                key=key + line[i];
            }           

            value = lines[q + 1];
            q++;

            dictionary.Add(key, value);
        }
    }

    foreach (string k in dictionary.Keys)
    {
        Console.WriteLine(k + " ==> " + dictionary[k]);
    }
Icemanind
  • 47,519
  • 50
  • 171
  • 296