如何匹配文本文件中的数据字段 [英] How to match data fields from text files

查看:266
本文介绍了如何匹配文本文件中的数据字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我目前有两个文本文件来自两个不同的系统,但它们有一个共同的商品代码。



我想知道如何读取这两个文本文件并匹配代码将两个文件中的数据写入另一个发送到第三方系统的文本文件?





例如文件1包含商品代码001的价格(500),而文件2包含商品代码001的描述(屏幕)



file3(我需要创建的文件)将包含以下内容

商品代码,描述,价格

001,屏幕,500

Hi
I currently have two text files which come from two different systems but they have a common itemcode.

I would like to know how could i read the two text files and match the codes to write data from the two files to another text file that gets sent to a third party system?


For example file 1 contains the price(500) of item code 001 while file 2 contains the description(Screen) of itemcode 001

the file3(which is the file i need to create) will have the following in it
itemcode,description,price
001,Screen,500

推荐答案

有很多方法可以做到这一点:但假设两个文件不一定是相同的顺序,我会创建一个包含Price和描述(可能称之为项目)然后创建项目词典:

There are a huge number of ways to do this: but assuming that the two files are not necessarily in the same order, I would create a class which contained the Price and Description (call it "Item" maybe) then create a Dictionary of Items:
private Dictionary<int, Item> items = new Dictionary<int, Item>();



我会依次读取每个文件,将其分为商品代码和价格或描述并对每行执行此操作:


I would then read each file in turn, breaking it into itemcode and either Price or Description and do this with each row:

if (!items.ContainsKey(itemcode))
   {
   items.Add(itemcode, new Item());
   }
//items[itemCode].Price = price;
//items[itemCode].Description = description;

取消注释适当的行。

最后,你有一个包含每个项目的词典,希望每个价格和描述都有。



这是一个但是存储数据的方法很差!

uncommenting the appropriate line.
At the end, you have a Dictionary which contains each Item, hopefully complete with each Price and Description.

It's a poor way to store your data though!


这篇关于如何匹配文本文件中的数据字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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