borisdj / fixedwidthparserwriter Goto Github PK
View Code? Open in Web Editor NEWReading & Writing fixed-width data (files) with defined field position in line or file(relative height).
License: MIT License
Reading & Writing fixed-width data (files) with defined field position in line or file(relative height).
License: MIT License
If I have a string containing fixed-field-length fields that are repeating, how can I parse that string line?
For example
"00050003T3A1111 2999BOSH 2100021 399APV 2100022 "
This string represents fields like:
4 byte long message length ("0005")
4 byte long message id ("0003")
1 byte long message type ("T")
1 byte long message sequence ("3")
8 byte long car Id ("A1111 ")
9 byte long part-1 price (" 2999")
30 byte long part-1 manufacturer ("BOSH ")
9 byte long part# ("2100021 ")
9 byte long part-2 price (" 399")
30 byte long part-2 manufacturer ("APV ")
9 byte long part# ("2100022 ")
Above, fields [part price], [part manufacturer], and [part#] are repeating and could be only 1 tripple or multiple tripples of these.
Also, the part has no decimal.
How would you parse this?
Obviously, using data model like in your documentation wont work as this data model expects a field to always start at same location; however, repeating fields above start on different locations:
public class InvoiceItem
{
[FixedWidthLineField(Start = 1, Length = 3)]
public int Number { get; set; }
[FixedWidthLineField(Start = 4, Length = 1)]
public string NumberedBullet { get; set; } = ".";
[FixedWidthLineField(Start = 5, Length = 30)]
public string Description { get; set; }
[FixedWidthLineField(Start = 35, Length = 6)]
public int Quantity { get; set; }
[FixedWidthLineField(Start = 41, Length = 13)]
public decimal Price { get; set; }
[FixedWidthLineField(Start = 54, Length = 13)]
public decimal Amount => Quantity * Price;
}
Parsing DateTime with custom format "mm.dd.yyyy" adds 1 minute to resulting value.
Tried Install-Package FixedWidthParserWriter in a .NETFramework 4.6.1 project and got the below error.
Install-Package : Could not install package 'FixedWidthParserWriter 1.0.8'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the
package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
Hi,
Is it possible to use variable class to FixedWidthLinesProvider?
Sample:
public enum A3Type
{
[StringValue("C")] Account,
[StringValue("0")] NotesWithoutVAT,
[StringValue("1")] NotesWithVAT,
[StringValue("9")] DetailWithVAT,
[StringValue(" ")] Dummy,
}
List<A3Type.Account> items0 = new FixedWidthLinesProvider<A3Type.Account>().Parse(element.lineValues);
It is not possible to add the non numeric pad on string values.
When I have a property value longer than the lenght, there should be an option if what to get an exception on this.
Ex.:
Property
[FixedWidthLineField(Start=1,Length=14)]
public string CNPJ { get; set; }
Program
static void Main(string[] args)
{
var p = new Pagamento(){CNPJ="123456789012345"};
var ps = new List<Pagamento>();
ps.Add(p);
p = new Pagamento(){CNPJ="12345678901234"};
ps.Add(p);
var ls = new FixedWidthLinesProvider<Pagamento>().Write(ps);
foreach (var l in ls)
{
Console.WriteLine(l);
}
}
Result
12345678901234
12345678901234
But there is a data truncation that I may be concerned in some use case.
I found your code via Reddit and it appears to work for what I want. My problem is the file I'm working on tries to stuff relational data into a text file. One line is the header, next line is a transaction, next line is a child of transaction and so on. When I read in the lines I have to parse one line at a time going into a different object. Now it's not a big deal to add my line to a list and parse the list and return the first item from the return list but just feels like a hack.
It would be too hard to add a parse single line, not sure if it would go in the same class or a different one called FixedWidthLineProvider
It could just have a call like:
public T Parse(string line, int structureTypeId)
{
return ParseData(new List { line }, FieldType.LineField));
}
Or something like that, maybe the base call ParseData could even be changed to handle one line rather than a list as well.
If destination model has DateTime? field, the parse method fails for empty string.
As I can see neither of types, which have nullable property, are not handled in Parse method.
What if I don't want to add FixedWidthLineField attributes to the class properties ? May be I can't modify the class or I need to define the Start and Length attributes at runtime. Is there any possibility to manually map the fields ?
Using the below code works on Android but fails on iOS.
` FileData fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
{
return; // user canceled file picking
}
List<string> list = new List<string>();
using (var reader = new StreamReader(fileData.GetStream()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line);
}
}
List<Animal> animals = new FixedWidthLinesProvider<Animal>().Parse(list);`
The stack trace is:
System.Exception: Operation is not supported on this platform.
at System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/corlib/System.Reflection.Emit/AssemblyBuilder.pns.cs:129
at FastMember.TypeAccessor.CreateNew (System.Type type, System.Boolean allowNonPublicAccessors) [0x00276] in <9d6126e91d244195bc86e41f41f850df>:0
at FastMember.TypeAccessor.Create (System.Type type, System.Boolean allowNonPublicAccessors) [0x00057] in <9d6126e91d244195bc86e41f41f850df>:0
at FixedWidthParserWriter.FixedWidthBaseProvider.ParseData[T] (System.Collections.Generic.List1[T] lines, FixedWidthParserWriter.FieldType fieldType) [0x0000d] in <2014c71297f64b8a965d79b875d9e4dd>:0 at FixedWidthParserWriter.FixedWidthLinesProvider
1[T].Parse (System.Collections.Generic.List`1[T] lines, System.Int32 structureTypeId) [0x0002c] in <2014c71297f64b8a965d79b875d9e4dd>:0
at BreedIQ.ImportViewModel.ImportPBB () [0x00135] in C:\inetpub\BreedIQ\BreedIQ\BreedIQ\ViewModels\ImportViewModel.cs:45
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.