c# - How to Convert plain text data into managed format? -
using c#, goal create working app accept text file input, read it, , separate sections of data within text file distinct groups contain index #, , name of section found in first part of section.
this perhaps more of question how take data in plain text file, separate sections of data "groups"? , output file in managed format.
the text input file has 13 fields, , additional field status, making 14 fields.
the text input file have 1500 - 2000 groups of sections of data, there 2000 entries indexed , name of entry being first field, "package name". section of data this:
package: horse version: 1.2.3 depends: libgcc provides: status: user installed other: other info other2: other info 2 package: cow version: 2.3.4 depends: libhay provides: milk status: user installed other: white black spots other2: has red cow bell around neck package: tractor version: 0.9.22 depends: diesel provides:
etc...
how can data read , placed appropriate tables or entries or datasets?
ps previous edit
a simplistic solution read file 1 line @ time, splitting @ ":" , storing each in dictionary<string, string>
stored in package
, adding list<package>
. whenever keyword "package", create new package
instance , add list. code below trick, not defensively coded.
static void main(string[] args) { string line; list<package> packages = new list<package>(); package package = null; streamreader file = new system.io.streamreader("textfile1.txt"); while ((line = file.readline()) != null) { string[] pair = line.split(new string[] { ": " }, stringsplitoptions.none); if (pair.length != 2) continue; if (pair[0] == "package") { package = new package(); packages.add(package); } package.values.add(pair[0].trim(), pair[1].trim()); } file.close(); console.writeline(packages.count); foreach (package p in packages) { console.writeline(p); foreach (keyvaluepair<string, string> pair in p.values) console.writeline("{0}={1}", pair.key, pair.value); } console.readline(); } } class package { public dictionary<string, string> values; public package() { values = new dictionary<string, string>(); } }
Comments
Post a Comment