如何在C#中解析txt文件 [英] How to Parse the txt file in c#

查看:712
本文介绍了如何在C#中解析txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是C#的新手,我必须分析格式化的文本文件,其中包含如下数据.

Hello I am new to C# and I have to parse the text file formatted which has the data like following.


H1|57535                 |65644474|       243.34
D1|671690160540      |FedEx Gnd   |Ground          |Parcel |06082016
D2|FCREADHCU3     |    10||||||     23.01
H1|57521                 |65642336|       923.31
D1|671690161010      |FedEx Gnd   |Ground          |Parcel |06082016
D2|PS121B         |     1|      0.00
H1|57521                 |65642336|       923.31
D1|671690161031      |FedEx Gnd   |Ground          |Parcel |06082016
D2|PS121B         |     1|      0.00
H1|57521                 |65642336|       923.31
D1|671690161020      |FedEx Gnd   |Ground          |Parcel |06082016
D2|PS121B         |     1|      0.00
snipping                

如何在C#中解析文本文件.帮助表示赞赏.

How can I parse text file in C#. Help Is appreciated.

推荐答案

从此开始:

var lines = File.ReadAllLines("<your path/filename>");
var stringBags = lines.Select(l => l.Split('|'));
var objects = stringBags.Select(b => new {Id = b[0], Name = b[1], SomeOtherField = b[2]});

这为您提供了一种解析文件的方法,并将其投影到您可以处理的某种对象中

This gives you a way to parse the file, and to project it into some sort of object you can deal with

这篇关于如何在C#中解析txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆