c# - Using TextFieldParser.FixedWidth in variable length strings -


i working on parser intended read in data in fixed-width format (8 char x 10 col). however, isn't case, , there valid data in areas not meet this. not safe assume there escape character (such + in figure below), 1 of several formats.

i had attempted using textfieldparser.fixedwidth, , giving 8x10 input, not meet quantity sent errorline instead.

  1. it doesn't seem practice parse exception catching block, it?

  2. since discrepant lines require additional work, brute force submethod best approach? of data comes in 8 char blocks. final block in line can tricky in may shorter if manually entered. (predicated on #1 being ok do)

  3. is there better tool using? feel i'm trying fit square peg in round hole fixedwidth textfieldparser.

note: delimited parsing not option, see 2nd figure.

edit clarification: text below pair of excerpts of input decks nastran, finite element code. aiming have generalized parsing method read text files in, , hand off split string[]s other methods process each card specific mapped object. (e.g. in image below, 2 object types rbe3 , set1)

extracted method:

    public static ienumerable<string[]> parsefixed(string filename, int width, int colcount)     {         var fieldarraylist = new list<string[]>();         using (var tfp = new textfieldparser(filename))         {             tfp.textfieldtype = fieldtype.fixedwidth;             var fieldwidths = new int[colcount];             (int = 0; < fieldwidths.length; i++)             {                 fieldwidths[i] = width;             }             tfp.commenttokens = new string[] { "$" };             tfp.fieldwidths = fieldwidths;             tfp.trimwhitespace = true;             while (!tfp.endofdata)             {                 try                 {                     fieldarraylist.add(tfp.readfields());                 }                 catch (microsoft.visualbasic.fileio.malformedlineexception ex)                 {                     debug.writeline(ex.tostring());                     // parse atypical lines here...?                     continue;                 }             }         }         return fieldarraylist;     } 

example data, (show characters turned on better visibility)

more example data


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -