如何传递csv文件的特定部分 [英] How to pass specific portion of the csv file

查看:78
本文介绍了如何传递csv文件的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我有一个csv文件,需要为特定部分解析它。由于我的csv文件结构不合理难以读入内存并识别行和列。



我的要求是,这里给出了两个帐户,需要根据它们进行解析课程结构





 ASSTROLLUPSTRT 
TOTALACCTS,总帐户: 2
ACCTHDR,帐户详细信息
ACCTHDR,大客户ID,帐户类型,价值,投资组合类型,投资组合ID,所有者ID
ACCTDTLS,2002-01 -20-12。 04 20 445297 ,RothIRA, 6280496 23 ,TaxExemptIRA, 1 ,<跨度类= 代码位> 2423
ACCTALLOHDR,账户分配
ACCTALLOHDR,钥匙帐户ID,类型,AssetClass,价值
ACCTALLODTLS,2002年-01-20-12。 04 20 445297
,分配,E, 0 11
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,成本基础,E, 0
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,分配,EA, 0 11
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,成本基础,EA, 0
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,分配,A, 2 92
ACCTALLODTLS,2002-01-20 -12。 04 20 445297 ,成本基础,A, 123328 9
ACCTALLODTLS,2002-01-20-12 。 04 20 445297 ,分配,AH, 2 92
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,成本基础,AH , 123328 9
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,分配,O, 56 72
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,成本基础,O, 0
ACCTALLODTLS,2002-01-20-12 。 04 20 445297 ,分配,OO, 56 72
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,Cost Basis,OO , 0
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,分配,L, 40 25
ACCTALLODTLS,2002-01-20-12。 04 20 445297 ,成本基础,L, 2528022 3
ACCTALLODTLS, 2002-01-20-12。 04 20 445297 ,分配,LC, 40 25
ACCTALLODTLS,2002-01 -20-12。 04 20 445297 ,Cost Basis,LC, 2528022 3
ACCTDTLS,2016-09-28 13 01 20 9473 ,应税, 0 ,应税, 2 , <跨度类= 代码位> 2423
ACCTALLOHDR,账户分配
ACCTALLOHDR,钥匙帐户ID,类型,AssetClass,价值
ACCTALLODTLS,2016年9月28日< span class =code-digit> 13 : 01 20 9473 ,分配,LC, 100
ACCTALLODTLS,2016-09-28 13 01 20 9473 ,成本基础,LC, 0
ACCTALLODTLS,2016-09-28 13 01 20 9473 ,分配,L, 100
ACCTALLODTLS,2016-09-28 13 01 20 9473 ,成本基础,L, 0





我的尝试:



下面是我通过查看csv文件定义的类。为了便于解析,如果需要更改,我们可以修改类结构。

请帮我实现这个目标..请详细说明如何继续进行

  public   class  CSVparsing 
{
public 列出AccountDetails< Item> {获取; set ; } // //在发布问题时小于和大于符号不起作用
}

public class AccountDetails
{
public string KeyAccountID { get ; set ; }
public string AccountType { get ; set ; }
public double 值{ get ; set ; }
public string PortfolioType { get ; set ; }
public double EarningsRate { get ; set ; }
public int PortfolioID { get ; set ; }
public int OwnerID { get ; set ; }
public 分配AllocDetails { get ; set ; }
}
public class 分配
{
public string AssetClassCode { get ; set ; }
public double 金额{ get ; set ; } // 分配值
public double CostBasis { get ; set ; } // 成本基础价值
}

解决方案

嗯,你需要做的第一件事是识别模式和代码。粗略一瞥之后,模式看起来像这样:



< br /> 
ASSTROLLUPSTRT< br />
&NBSP;&NBSP; ACCTHDR [段名] LT峰; br />
&NBSP;&NBSP;&NBSP;&NBSP; ACCTHDR [列名] LT峰; br />
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; ACCTDTLS,详细数据...<峰; br />
&NBSP;&NBSP;&NBSP;&NBSP; ACCTALLOHDR [子报头名称]<峰; br />
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; ACCTALLOHDR [列名] LT峰; br />
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; ACCTALLODTLS,详细数据...<峰; br />
&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; ACCTALLODTLS,详细数据...<峰; br />
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; ACCTALLODTLS,详细数据...< br />





一旦建立了模式,就编写代码来支持模式。不幸的是,根据CSV文件的严格定义,这不是CSV文件(所有行都具有相同的列数)。这看起来更像是没有XML装饰的XML文件。部分命名是不方便的,但不是一个实质性的心理障碍。出去和代码。


我自己找到的解决方案。请任何人建议任何简单或简单的方法

  class 计划
{
< span class =code-keyword> static void Main( string [] args)
{
var lines = File.ReadAllLines( @ Mycsvfile.csv)。选择(x = > x.Split( ' ,'))。ToList();
列表< accountdetails> lstAccountDetails = new List< accountdetails>();
for int i = 0 ; i < lines.Count; i ++)
{
if (lines [i] [ 0 ]。修剪()== ACCTDTLS
{
AccountDetails ad = new AccountDetails();
if (lines [i] .Count()> 6
{
ad.KeyAccountID = lines [i] [ 1 ];
ad.AccountType = lines [i] [ 2 ];
ad.Value = Convert.ToDouble(lines [i] [ 3 ]);
ad.PortfolioType = lines [i] [ 4 ];
ad.PortfolioID = Convert.ToInt32(lines [i] [ 5 ]);
ad.OwnerID = Convert.ToInt32(lines [i] [ 6 ]);
}
i = i + 3 ;
列表< allocation> lstallocation = new List< allocation>();
for int j = i; j < i + 2 ; j ++)
{
Allocation aloc = new Allocation();
if (j < lines.Count)
{
aloc.AssetClassCode = lines [j] [ 3 ];
if (lines [j] [ 0 ]。修剪()== ACCTALLODTLS
{
aloc.Amount = Convert.ToDouble(lines [j] [ 4 ]。Trim());
aloc.CostBasis = Convert.ToDouble(lines [j + 1 ] [ 4 ]。修剪());
j = j + 1 ;
lstallocation.Add(aloc);
i = j + 1 ;
}
else
{
i = i - 1 ;
break ;
}
}
其他
{
break < /跨度>;
}
}
ad.AllocDetails = lstallocation;
lstAccountDetails.Add(ad);
}
}
Console.ReadLine();
}
}
< / 分配 > < / 分配 > < / accountdetails > < / accountdetails >


Hi Friends,

I have a csv file and need to parse it for the specific portion. Since my csv file is not properly structured difficult to read into memory and identify rows and columns.

My requirement is, Here Two accounts are given and need to parsed based upon the class structure


ASSTROLLUPSTRT
TOTALACCTS ,  Total Accounts : 2
ACCTHDR , Account Details
ACCTHDR , Key Account ID, Account Type, Value, Portfolio Type, Portfolio ID, Owner ID
ACCTDTLS ,  2002-01-20-12.04.20.445297, RothIRA, 6280496.23, TaxExemptIRA, 1, 2423
ACCTALLOHDR, Account Allocations
ACCTALLOHDR, Key Account ID, Type , AssetClass, Value
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, E, 0.11
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, E, 0
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, EA, 0.11
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, EA, 0
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, A, 2.92
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, A, 123328.9
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, AH, 2.92
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, AH, 123328.9
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, O, 56.72
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, O, 0
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, OO, 56.72
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, OO, 0
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, L, 40.25
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, L, 2528022.3
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Allocation, LC, 40.25
ACCTALLODTLS ,  2002-01-20-12.04.20.445297, Cost Basis, LC, 2528022.3
ACCTDTLS ,  2016-09-28 13:01:20:9473, Taxable, 0, Taxable, 2, 2423
ACCTALLOHDR, Account Allocations
ACCTALLOHDR, Key Account ID, Type , AssetClass, Value
ACCTALLODTLS ,  2016-09-28 13:01:20:9473, Allocation, LC, 100
ACCTALLODTLS ,  2016-09-28 13:01:20:9473, Cost Basis, LC, 0
ACCTALLODTLS ,  2016-09-28 13:01:20:9473, Allocation, L, 100
ACCTALLODTLS ,  2016-09-28 13:01:20:9473, Cost Basis, L, 0



What I have tried:

Below is the class i defined by looking at the csv file. for easy parsing if changes are required we can modify the class structure.
Please help me to achieve this..Please shed some lights how to proceed further

    public class CSVparsing
{
    public List AccountDetails<Item> { get; set; } // //less than and greater symbol is not working while posting the question
}

public class AccountDetails
{
    public string  KeyAccountID { get; set; }
    public string  AccountType { get; set; }
    public double Value { get; set; }
    public string  PortfolioType { get; set; }
    public double EarningsRate { get; set; }
    public int  PortfolioID { get; set; }
    public int  OwnerID { get; set; }
    public Allocation AllocDetails { get; set; }
}
public class Allocation
{
    public string AssetClassCode { get; set; }
    public double Amount { get; set; } //Allocation Value
    public double CostBasis { get; set; } // Cost Basis Value
}

解决方案

Well, the first thing you need to do is identify the pattern(s), and code for that. After a cursory glance, the pattern looks like this:

<br />
ASSTROLLUPSTRT<br />
  ACCTHDR [section name]<br />
    ACCTHDR [column names]<br />
      ACCTDTLS, Detail data...<br />
    ACCTALLOHDR [subheader name]<br />
      ACCTALLOHDR[column names]<br />
        ACCTALLODTLS, Detail data...<br />
        ACCTALLODTLS, Detail data...<br />
        ACCTALLODTLS, Detail data...<br />



Once you've established the patterns, write code to support the patterns. Unfortunately, this is NOT a CSV file according to the strict definition of a CSV file (all rows have the same number of columns). This looks more like an XML file without the XML decoration. The section naming is inconvenient, but not a substantial mental hurdle. Go forth and code.


Myself found solution. Please anyone suggest any simple or easy way

 class Program
    {
        static void Main(string[] args)
        {
            var lines = File.ReadAllLines(@"Mycsvfile.csv").Select(x => x.Split(',')).ToList();
            List<accountdetails> lstAccountDetails = new List<accountdetails>();
            for (int i = 0; i < lines.Count; i++)
            {
                if (lines[i][0].Trim() == "ACCTDTLS")
                {
                    AccountDetails ad = new AccountDetails();
                    if (lines[i].Count() > 6)
                    {
                        ad.KeyAccountID = lines[i][1];
                        ad.AccountType = lines[i][2];
                        ad.Value = Convert.ToDouble(lines[i][3]);
                        ad.PortfolioType = lines[i][4];
                        ad.PortfolioID = Convert.ToInt32(lines[i][5]);
                        ad.OwnerID = Convert.ToInt32(lines[i][6]);
                    }
                    i = i + 3;
                    List<allocation> lstallocation = new List<allocation>();
                    for (int j = i; j < i + 2; j++)
                    {
                        Allocation aloc = new Allocation();
                        if (j < lines.Count)
                        {
                            aloc.AssetClassCode = lines[j][3];
                            if (lines[j][0].Trim() == "ACCTALLODTLS")
                            {
                                aloc.Amount = Convert.ToDouble(lines[j][4].Trim());
                                aloc.CostBasis = Convert.ToDouble(lines[j + 1][4].Trim());
                                j = j + 1;
                                lstallocation.Add(aloc);
                                i = j + 1;
                            }
                            else
                            {
                                i = i - 1;
                                break;
                            }                           
                        }
                        else
                        {
                            break;
                        }                      
                    }
                    ad.AllocDetails = lstallocation;
                    lstAccountDetails.Add(ad);
                }
            }
            Console.ReadLine();
        }
    }
</allocation></allocation></accountdetails></accountdetails>


这篇关于如何传递csv文件的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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