CSV文件解析



由于这是第一个讨论主题,如果我说错了,请提前原谅。我的问题是,我有一个CSV文件的例子,格式为'.txt',我正在使用这个文件。

<
  • 名称/gh><
  • 地址/gh>
  • <
  • 状态/gh>

我想得到这样的值。我使用了CSVHelper,要么我做不到,要么我想要的格式不在那个包中。提前感谢那些会帮助我的人。附件是我要解析的文件;

姓名,地址,方向,状态,持续时间,日期Berat Bey Dörtsan,"05315554622",出局,未应答,00:00,00:00,8/4/2022 (9:25:48 AM)Berat Bey Dörtsan,"05315554622",出局,回答,00:47,00:47,8/4/2022 (9:27:55 AM)Berat Bey Dörtsan,"05315554622",来电,应答,00:54,00:54,8/4/2022(上午9:35:02)Berat Bey Dörtsan,"05315554622",出线,应答,00:19,00:19,8/4/2022(下午2:58:43)Berat Bey Dörtsan,"05315554622",出局,回答,00:49,00:49,8/5/2022(上午9:21:52)Berat Bey Dörtsan,"05315554622",来电,回答,01:56,01:56,8/16/2022 (10:17:55 AM)Berat Bey Dörtsan,"05315554622",出局,回答,00:47,00:47,9/7/2022 (11:02:33 AM)Berat Bey Dörtsan,"05315554622",出线,已接,01:19,01:19,9/7/2022 (11:04:35 AM)Berat Bey Dörtsan,"05315554622",出线,已接,01:57,01:57,9/7/2022 (11:07:20 AM)Berat Bey Dörtsan,"05315554622",出线,应答,04:59,04:59,9/7/2022 (11:12:54 AM)Berat Bey Dörtsan,"05315554622",出局,应答,01:50,01:50,9/7/2022 (11:18:36 AM)Berat Bey Dörtsan,"05315554622",来电,回答,00:37,00:37,9/7/2022 (11:36:36 AM)Berat Bey Dörtsan,"05315554622",出局,应答,00:59,00:59,9/7/2022 (11:55:19 AM)Berat Bey Dörtsan,"05315554622",来电,未接,00:00,00:00,2022年7月9日(下午12:15:26)Berat Bey Dörtsan,"05315554622",出线,未应答,00:00,00:00,9/7/2022 (12:21:12 PM)Berat Bey Dörtsan,"05315554622",出线,未应答,00:00,00:00,9/7/2022 (12:21:24 PM)Berat Bey Dörtsan,"05315554622",外向,回答,00:18,00:18,9/7/2022 (12:36:08 PM)

我试过这样做;

  • Name = Berat Bey Dörtsan
  • 地址= 05315554466
  • 方向=出方向
  • 状态=未答复
  • 持续时间= 00:54 - 01:56
  • 日期= 8/4/2022 (9:25:48 AM)

and I was used these codes;

using CsvHelper;
using CsvHelper.Configuration.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TextVeriCekme
{
    public partial class Form1 : Form
    {
        String csvPath = "";
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog
            {
                Title = "CSV Dosyası Aç",
                Filter = "csv files (*.csv)|*.csv",
                CheckFileExists = true,
                CheckPathExists = true
            };
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(ofd.FileName);
                csvPath = ofd.FileName;
            }
        }
        public class Foo
        {
            public string Name { get; set; }
            public string Address { get; set; }
            public string Direction { get; set; }
            public string Status { get; set; }
            public string Duration { get; set; }
            [Name("Date")]
            [Format("dd-MM-yyyy")]
            public DateTime Date { get; set; }
        }
        int sayac = 0;
        private void button2_Click(object sender, EventArgs e)
        {
            IEnumerable<Foo> records = null;
            List<Foo> list = null;
            using (var reader = new StreamReader(csvPath))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                records = csv.GetRecords<Foo>();
                list = records.ToList();
                MessageBox.Show("Toplam Kayıt : " + records.Count().ToString());
                foreach (Foo record in list)
                {
                    if (sayac == 1)
                    {
                        MessageBox.Show(record.Name);
                        MessageBox.Show(record.Address);
                        MessageBox.Show(record.Direction);
                        MessageBox.Show(record.Status);
                        MessageBox.Show(record.Duration);
                        MessageBox.Show(record.Date.ToString());
                    }
                    sayac++;
                }
            }
        }
    }
}

这样我就不能得到'Date'变量了。一般来说,我想做的是一个Windows窗体来保存这些呼叫日志(日期和时间)。

Foo类型中日期的格式装饰错误。日期值如下所示:

M/d/yyyy (h:mm:ss tt)

所以你需要相应地装饰字体:

public class Foo
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string Direction { get; set; }
    public string Status { get; set; }
    public string Duration { get; set; }
    [Name("Date")]
    [Format("M/d/yyyy (h:mm:ss tt)")]
    public DateTime Date { get; set; }
}

如果您不能更改属性,您将需要提供一个ClassMap配置,如下所示:

https://stackoverflow.com/a/62536666/3043

部分问题是Duration是数据中的两列,因为它没有被括在双引号中。

00:47,00:47代替"00:47,00:47"

因此,你不能按照标题名称,而是需要使用ClassMap来映射Index的属性。

void Main()
{
    using (var reader = new StreamReader(@"C:TempCsvFileParsing.txt"))
    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
    {
        csv.Context.RegisterClassMap<FooMap>();
        
        var records = csv.GetRecords<Foo>().ToList();
    }
}
public class FooMap : ClassMap<Foo>
{
    public FooMap()
    {
        Map(x => x.Name).Index(0);
        Map(x => x.Address).Index(1);
        Map(x => x.Direction).Index(2);
        Map(x => x.Status).Index(3);
        Map(x => x.Duration).Convert(args => args.Row.GetField(4) + " - " + args.Row.GetField(5));
        Map(x => x.Date).Index(6).TypeConverterOption.Format("M/d/yyyy (h:mm:ss tt)");
    }
}
public class Foo
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string Direction { get; set; }
    public string Status { get; set; }
    public string Duration { get; set; }
    public DateTime Date { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新