-3

Lacking the know how to read the data that is being fed into a text file, here is what I have so far

try
{

    StreamReader sr = new StreamReader(@"C:\\Program Files (x86)\\Python Data\\Rolldata.txt");

    line = sr.ReadLine();
    while (line != null)
    {
        Console.WriteLine(line);
        textBox1.Text = line;
        line = sr.ReadLine();
    }

    sr.Close();
    Console.ReadLine();

}

This simply extracts the data and put it's to a writeline so I am able to physically see it, but how do I parse the data in a way that specific pairs of data such as {id: 1} or {foobar:foobar} can be read with only their specific attached data?

Due to requests, I will elaborate and show the data

{'updated_at': None, 'roll': 3, 'hash': 'f114b7a0fd714256015b5a420ebe974f3977c53d3a3423f8532b58b69a6f3aa5', 'state': 3, 'created_at': None, 'id': 9947837}{'updated_at': None, 'roll': 9, 'hash': '4e2a94657c9ca2c9206369d64fb84b03520875f870389bee1604fa4f74ef0cfa', 'state': 3, 'created_at': None, 'id': 9947838}{'updated_at': None, 'roll': 0, 'hash': 'd5dc5c3378724deb071ae6bdaa0cfb05222db68e4afc441d9138f0e84609fc4c', 'state': 3, 'created_at': None, 'id': 9947839}

I want to extract the id and the roll and pair them together if possible

Noah Linton
  • 11
  • 1
  • 8
  • Read file into `string`, then `String.Split` or `Regex`. – Lei Yang Dec 20 '16 at 01:36
  • You need to show us the data if you expect us to tell you how to parse it. `@"C:\\Program Files (x86)\\Python Data\\Rolldata.txt` is wrong, it should be either `"C:\\Program Files (x86)\\Python Data\\Rolldata.txt` or `@"C:\Program Files (x86)\Python Data\Rolldata.txt`. Also, why is your data in the `C:\Program Files (x86)\` path? I hope you don't think users can typically write there. – Dour High Arch Dec 20 '16 at 02:00
  • Technically speaking, you've already succeeded at reading the data. I think what you are asking is "how do I create a representation of the data in c# variables," e.g. create a collection of structs or classes corresponding to the data in the file. The answer, of course, depends on the way you wish to represent the data and how you are going to use it, which you have not specified. – John Wu Dec 20 '16 at 02:12
  • Look like Json, so I'd suggest using a library made for parsing Json data – juharr Dec 20 '16 at 02:52

2 Answers2

0

I find it handy with File.ReadAllText like

string YourTextFileContent = File.ReadAllText(@"C:\\Program Files (x86)\\Python Data\\Rolldata.txt");
Console.WriteLine(YourTextFileContent);

and you can post the content of the file and your expected output to read data in pairs. Whereas Split, Regex are going to help you read that kind of data but I think your string might be a JSON string and thus it could be much more easier way to get the data.

Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

Your data seem like json format, but with a bit syntax issue.

So first I suggest you ref http://www.json.org/ and fix your data to be like

[
    { 
        'updated_at': '2016-12-20', 
        'roll': 3, 
        'hash': 'f114b7a0fd714256015b5a420ebe974f3977c53d3a3423f8532b58b69a6f3aa5', 
        'state': 3, 
        'created_at': '2016-12-20', 
        'id': 9947837
    },
    { 
        'updated_at': '2016-12-20', 
        'roll': 9, 
        'hash': '4e2a94657c9ca2c9206369d64fb84b03520875f870389bee1604fa4f74ef0cfa', 
        'state': 3, 
        'created_at': '2016-12-20', 
        'id': 9947838
    },
    { 
        'updated_at': '2016-12-20', 
        'roll': 0, 
        'hash': 'd5dc5c3378724deb071ae6bdaa0cfb05222db68e4afc441d9138f0e84609fc4c', 
        'state': 3, 
        'created_at': '2016-12-20', 
        'id': 9947839
    }
]

Secondly, create a C# class for your json

public class PropertiesYouWant
{
    public int roll { get; set; }
    public int id { get; set; }
}

Finally, use Newtonsoft.Json to Deserialize json string as follow

var result = JsonConvert.DeserializeObject<List<PropertiesYouWant>>(@"json above");

The result should be the collection you want.

Circle Hsiao
  • 1,497
  • 4
  • 22
  • 37
  • When you use the @"json above" what should I do if I am importing the data from an external source such as a text file? – Noah Linton Dec 20 '16 at 18:31
  • In that case read the string first and do some pre-processing so it can fit json format. For your example, `"[" + srcText.Replace("}", "},").Replace("None", "''") + "]"` – Circle Hsiao Dec 21 '16 at 01:24