2 .CSV文件,Substract 2;分隔字段 [英] 2 .CSV Files, Substract 2 ; delimited Fields

查看:90
本文介绍了2 .CSV文件,Substract 2;分隔字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

上次启动c#,但现在我遇到了问题...我需要一个片段2了解如何操作。



问题:



 private void button1_Click(object sender,EventArgs e)
{
using( StreamWriter writer = new StreamWriter(@C:/WEBUPLOAD/UPLOAD/Output.csv,true))
{
using(StreamReader reader = new StreamReader(@C:/input.csv, System.Text.Encoding.Default))
{
string line;
while((line = reader.ReadLine())!= null)
{
string [] items = line.Replace(\,)。Trim() .Split('';'');
if(items.Length!= 4)continue;
int i1,i2;
bool b1 = int.TryParse(items [2], out i1);
bool b2 = int.TryParse(items [3],out i2);
items [2] = Math.Max(0,i1 -i2).ToString();
line = String.Join(〜,items,0,3);
writer.WriteLine(line);
}







我想重写上面的代码,但现在不用两个文件怎么做。







我得到一个看起来像这样的文件:



;; 0

09000;目录1; 10148

09001;目录2; 20

09002;目录3; 166



我得到一个看起来像这样的2文件:



09000; 476

09003; 10

09005; 5



怎么办?



输出文件必须如下:



09000~目录1~9672

09001~目录2~10

09002~目录3~161



我需要从File1中减去字段3,从文件2中获取字段2并得到如上所述的输出文件。

解决方案

我会使用数据库为了那个原因。如果你还没有连接到服务器,只需使用一些嵌入式版本(有几个从SQL CE到SQLite等等)。如果您使用的是某些MSSQL版本,请创建一个包含两个临时表的会话。

现在,使用您的代码或其他代码来读取CSV并将数据插入两个表中。进行正确的连接并选择结果。比删除数据。就是这样。



您可以使用list和linq来管理内存中的所有内容,但这取决于您必须处理的数据量。 br />


如果您有大量数据,并且sql服务器是远程的,您仍然可以使用嵌入式方法完成此任务。


< blockquote>不,我想像上面那样做..没有sql,不能这样做..


Hi all,
started c# last time, but now i got a Problem...i Need a snippet 2 understand how to to it.

Problem:

private void button1_Click(object sender, EventArgs e)
        {
            using (StreamWriter writer = new StreamWriter(@"C:/WEBUPLOAD/UPLOAD/Output.csv", true))
            {
                using (StreamReader reader = new StreamReader(@"C:/input.csv", System.Text.Encoding.Default))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] items = line.Replace("\"", "").Trim().Split('';'');
                        if (items.Length != 4) continue;
                        int i1, i2;
                        bool b1 = int.TryParse(items[2], out i1);
                        bool b2 = int.TryParse(items[3], out i2);           
                        items[2] = Math.Max(0, i1 -i2).ToString();
                        line = String.Join("~", items, 0, 3);
                        writer.WriteLine(line);
                    }




I wanna rewrite the Code above, but dont now how to do that with two files.



I get a 1 File that looks like that:

"";"";0
" 09000";"Catalog 1";10148
" 09001";"Catalog 2";20
" 09002";"Catalog 3";166

I get a 2 File that looks like that:

" 09000";476
" 09003";10
" 09005";5

What to do?

the Output file must look like that:

09000~Catalog 1~9672
09001~Catalog 2~10
09002~Catalog 3~161

I need to substract Field 3 from File1, with Field 2 from File 2 and get a Output File like above.

解决方案

I would use a database for that. If you don''t connect already to a server, just use some of the embedded editions (there are several from SQL CE to SQLite and so on...). If you are using some MSSQL edition, create a session with two temporary tables.
Now, use your code, or some other code to read the CSV and insert the data in the two tables. Make a proper join and select the result. Than delete the data. That''s it.

You could use lists and linq to manage all this in memory, but it depends on the amount of the data you have to process.

If you have lot of data, and an sql server that is remote, you can still use the embedded approach for this task.


nah i wanna do it like above..without sql, cant do that..


这篇关于2 .CSV文件,Substract 2;分隔字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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