从一个CSV文件中获取一行以在其他CSV文件中搜索匹配行

发布于 2025-01-31 17:51:17 字数 548 浏览 3 评论 0原文

我偶然发现了一个问题,即在CSVhelper的帮助下生成了CSV文件。该文件由2列(时间戳和ID)组成。我现在想打开一个包含这些ID的ID和说明的不同CSV文件,并将描述导入其他CSVFILE,以将ID与描述匹配。

但是,我还没有找到有关如何执行此操作的任何信息。并想知道是否有任何顺利执行此操作的方法。

编辑:

要澄清:我有一个CSV文件,其中包含与此相似的不同事件:

17/05/2022 16.28, 2 
17/05/2022 16.28, 3 

其中1,2,3和4表示事件发生的事件的ID。我想从其他CSV文件中获取ID的描述。看起来像这样:

1, problem 1
2, problem 2
3, problem 3
4, problem 4

所以我想获取ID 2和3的描述,然后将其插入ID旁边,以便最终结果看起来像这样:

17/05/2022 16.28, 2, problem 2
17/05/2022 16.28, 3, problem 3

I have stumbled upon a problem where i have a csv file generated with the help of CSVhelper. The file consists of 2 columns (Timestamp and ID). I would now like to open a different CSV file that contains IDs and descriptions for these IDs and import the description to the other CSVfile to match the ID with a description.

I have not however found any information on how to perform this. And wondered if there is any smooth way of performing this.

EDIT:

To clarify: I have one CSV file which contains different events which looks similar to this:

17/05/2022 16.28, 2 
17/05/2022 16.28, 3 

Where the 1,2,3 and 4 represents the ID of the event that has happened. I would like to fetch a description for the ID from a different CSV file. Which looks like this:

1, problem 1
2, problem 2
3, problem 3
4, problem 4

So I would like to fetch the description for the ID 2 and 3 and insert it next to the ID so the end result would look like this:

17/05/2022 16.28, 2, problem 2
17/05/2022 16.28, 3, problem 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一腔孤↑勇 2025-02-07 17:51:17

@Peter Smith有正确的主意。这样的事情应该起作用。

void Main()
{
    List<Foo> records;

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        HasHeaderRecord = false
    };

    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader, config))
    {
        var options = new TypeConverterOptions { Formats = new[] { "dd/MM/yyyy mm.ss" } };

        csv.Context.TypeConverterOptionsCache.AddOptions<DateTime>(options);
        
        csv.Context.RegisterClassMap<FooMap>();

        records = csv.GetRecords<Foo>().ToList();
    }

    using (var reader = new StreamReader("path\\to\\descriptions.csv"))
    using (var csv = new CsvReader(reader, config))
    {
        var descriptions = csv.GetRecords<dynamic>().ToList();

        records = records.Join(
            descriptions, 
            record => record.Id, 
            description => description.Field1, 
            (record, description) => { record.Description = description.Field2; return record; }).ToList();
    }
    
    using (var writer = new StreamWriter("path\\to\\file.csv"))
    using (var csv = new CsvWriter(writer, config))
    {
        var options = new TypeConverterOptions { Formats = new[] { "dd/MM/yyyy mm.ss" } };

        csv.Context.TypeConverterOptionsCache.AddOptions<DateTime>(options);
        
        csv.WriteRecords(records);
    }
}

public class FooMap : ClassMap<Foo>
{
    public FooMap() 
    {
        Map(x => x.Id).Index(1);
        Map(x => x.Timestamp).Index(0);
    }
}

public class Foo
{
    [Index(1)]
    public string Id { get; set; }
    [Index(0)]
    public DateTime Timestamp { get; set; }
    [Index(2)]
    public string Description { get; set; }
}

@Peter Smith has the right idea. Something like this should work.

void Main()
{
    List<Foo> records;

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        HasHeaderRecord = false
    };

    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader, config))
    {
        var options = new TypeConverterOptions { Formats = new[] { "dd/MM/yyyy mm.ss" } };

        csv.Context.TypeConverterOptionsCache.AddOptions<DateTime>(options);
        
        csv.Context.RegisterClassMap<FooMap>();

        records = csv.GetRecords<Foo>().ToList();
    }

    using (var reader = new StreamReader("path\\to\\descriptions.csv"))
    using (var csv = new CsvReader(reader, config))
    {
        var descriptions = csv.GetRecords<dynamic>().ToList();

        records = records.Join(
            descriptions, 
            record => record.Id, 
            description => description.Field1, 
            (record, description) => { record.Description = description.Field2; return record; }).ToList();
    }
    
    using (var writer = new StreamWriter("path\\to\\file.csv"))
    using (var csv = new CsvWriter(writer, config))
    {
        var options = new TypeConverterOptions { Formats = new[] { "dd/MM/yyyy mm.ss" } };

        csv.Context.TypeConverterOptionsCache.AddOptions<DateTime>(options);
        
        csv.WriteRecords(records);
    }
}

public class FooMap : ClassMap<Foo>
{
    public FooMap() 
    {
        Map(x => x.Id).Index(1);
        Map(x => x.Timestamp).Index(0);
    }
}

public class Foo
{
    [Index(1)]
    public string Id { get; set; }
    [Index(0)]
    public DateTime Timestamp { get; set; }
    [Index(2)]
    public string Description { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文