c# - 我如何解析这个 CSV 文件,因为它有很多逗号,我需要 3 个部分来检索信息?
问题描述
我正在练习解析文本和 csv 文件。我有一个关于女巫的特定 csv 文件,我已经尝试解析了几个小时,但是我无法找到正确的逻辑,因为根据解析 19 行,我需要检索前 2 行的某些信息我必须在下面显示所有信息。csv 文件看起来像这样
Titular,Cuenta #,Moneda,Fecha del reporte,,
MyCompany,123654897,Dollar,26/6/2010,,
,,,,,
Períod,,,,,
From:,31/01/2010,Until:,25/06/2019,,
,,,,,
Date,Description,Númber reference,Débit,Crédit,Balance
31/01/2019,Credito por Intereses,710504251714-50398277,,132.16,"3,073.55"
8/2/2019,Depositos,S9091B19,,"74,258.74","9,722.29"
8/2/2019,ACH Debito,F1999ZV,"8,748.35",,"2,073.55"
14/02/2019,Creditos Varios,F90BRF,,"7,429.77","4,391.32"
18/02/2019,ACH Debito,FT0SMD,"4,824.77",,"3,073.55"
21/02/2019,ACH Credito,F8PH,,"8,000.98","3,673.53"
21/02/2019,Transferencia entre Cuentas,2DMFRG,"7,005.96",,"2,667.57"
22/02/2019,Pago de Comision,R2SHX,20.00,,"5,647.57"
25/02/2019,ACH Credito,FT1905,,"4,083.08","4,490.65"
25/02/2019,Transferencia entre Cuentas,FT254354,"4,437.10",,"3,053.55"
25/02/2019,ACH Credito,ASF455MZQT,,222.15,"3,675.70"
25/02/2019,Transferencia entre Cuentas,GHVF456Q1XLG,"5,536.33",,"5,453.55"
我已经运行了一些测试并一直在调试以查看行为并问自己;我是否需要指定我所在的行,以便我检索信息?如果是这样,我该怎么做?
这是我的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace T_2060_ParserEstadoDeCuenta
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Parsing the csv file");
List<clsEstadoCuenta> resp = new List<clsEstadoCuenta>();
var lines = File.ReadAllLines("d:\\ztemp\\parseEstcta.csv");
for (int i = 1; i < lines.Count(); i++)
{
try
{
/*
*/
var campos = lines[i].Split(',');
clsEstadoCuenta nR = new clsEstadoCuenta();
nR.NumeroCuenta = (String.IsNullOrEmpty(campos[1])) ? "" : campos[1];
nR.CodigoPais = 504;
nR.Banco = "Fichosa";
nR.Moneda = (String.IsNullOrEmpty(campos[2])) ? "" : campos[2];
nR.TasaCambio = 24.6;
var tmpFecha = campos[0].Split('/');
nR.FechaTransaccion = new DateTime(Convert.ToInt32(tmpFecha[2]), Convert.ToInt32(tmpFecha[1]), Convert.ToInt32(tmpFecha[0]));
nR.Descripcion = (String.IsNullOrEmpty(campos[1])) ? "" : campos[1];
nR.Referencia = (String.IsNullOrEmpty(campos[2])) ? "" : campos[2];
nR.Debito = (String.IsNullOrEmpty(campos[4])) ? 0 : Convert.ToDouble(campos[4]);
nR.Credito = (String.IsNullOrEmpty(campos[5])) ? 0 : Convert.ToDouble(campos[5]);
nR.Payee = "A";
}
catch (Exception ex)
{
Console.WriteLine("error on line {0} : {1}", i, ex.Message);
continue;
}
}
Console.WriteLine("Parsing has ended, we have {0} rows \n", resp.Count);
foreach (var item in resp)
{
Console.WriteLine(item.NumeroCuenta+"\t" +item.CodigoPais+"\t"+item.Banco+"t"+item.Moneda+"\t"+item.Debito);
}
Console.ReadLine();
}
class clsEstadoCuenta
{
private string _NumeroCuenta;
public string NumeroCuenta
{
get { return _NumeroCuenta; }
set { _NumeroCuenta = value; }
}
private int _CodigoPais;
public int CodigoPais
{
get { return _CodigoPais; }
set { _CodigoPais = value; }
}
private string _Banco;
public string Banco
{
get { return _Banco; }
set { _Banco = value; }
}
private string _Moneda;
public string Moneda
{
get { return _Moneda; }
set { _Moneda = value; }
}
private double _TasaCambio;
public double TasaCambio
{
get { return _TasaCambio; }
set { _TasaCambio = value; }
}
private double _Debito;
public double Debito
{
get { return _Debito; }
set { _Debito = value; }
}
private double _Credito;
public double Credito
{
get { return _Credito; }
set { _Credito = value; }
}
private DateTime _FechaTrasaccion;
public DateTime FechaTransaccion
{
get { return _FechaTrasaccion; }
set { _FechaTrasaccion = value; }
}
private string _Payee;
public string Payee
{
get { return _Payee; }
set { _Payee = value; }
}
private string _Descripcion;
public string Descripcion
{
get { return _Descripcion; }
set { _Descripcion = value; }
}
private string _Referencia;
public string Referencia
{
get { return _Referencia; }
set { _Referencia = value; }
}
private string _CodigoBancario;
public string CodigoBancario
{
get { return _CodigoBancario; }
set { _CodigoBancario = value; }
}
private string _Categoria;
public string Categoria
{
get { return _Categoria; }
set { _Categoria = value; }
}
private string _Sector;
public string Sector
{
get { return _Sector; }
set { _Sector = value; }
}
private double _ValorLocal;
public double ValorLocal
{
get
{
_ValorLocal = Credito - Debito;
return _ValorLocal;
}
//set { _ValorLocal = value; }
}
private double _ValorDolares;
public double ValorDolares
{
get
{
_ValorDolares = ValorLocal / TasaCambio;
return _ValorDolares;
}
// set { _ValorDolares = value; }
}
private string _NombreEmpresa;
public string NombreEmpresa
{
get { return _NombreEmpresa; }
set { _NombreEmpresa = value; }
}
}
}
}
解决方案
问题是您尝试通过拆分“,”来分隔单元格,但部分单元格包含“,”字符。示例:“9,722.29”。
推荐阅读
- sql - Oracle 合并 - 我可以执行“当不匹配时删除”之类的操作吗?
- sql - 如何通过比较几个字段而不是其他字段来进行 JOIN
- list - 在列内,在 PySpark 列表中的两个元素之间创建一个范围
- javascript - 使用 PDF-LIB 在 React 中渲染 PDF
- matrix - 计算 PowerBI 矩阵中相邻列的差异
- python - 如何在 Python 中以更简洁的方式解析此文本?
- ios - 如果安装了 Facebook 应用程序,Firebase iOS Facebook 身份验证将不起作用
- sql - 使用 Presto 从数据构造 json
- java - 是否可以创建一个准备好的语句并稍后在 postgres 下使用 Java 重用它?
- libgit2 - 当用户 git_merge 在 libgit2 中失败时如何输出通知回调?