简体   繁体   English

如何使用 CsvHelper 库读取没有引用的 csv 文件?

[英]How to read no quoted csv file using CsvHelper library?

I need to read csv file content using CsvHelper library.我需要使用 CsvHelper 库读取 csv 文件内容。 Here's my method responsible for reading a file:这是我负责读取文件的方法:

        public virtual async Task<ICollection<Shipment>> GetMaterialReleaseReceiptsAsync(string filePath)
        {
            var validShipmentData = new List<Shipment>();

            try
            {
                using var reader = new StreamReader(filePath);
                using var csvReader = new CsvReader(reader, CultureInfo.InvariantCulture);

                csvReader.Configuration.Delimiter = ";";
                csvReader.Configuration.HeaderValidated = null;
                csvReader.Configuration.MissingFieldFound = null;

                var shipment = await csvReader
                    .GetRecordsAsync<ShipmentData>()
                    .Select(s => new Shipment
                        {
                            Issue = s.Issue,
                            MaterialReleaseReceipt = s.MaterialReleaseReceipt,
                            FileLocation = filePath
                        })
                    .ToListAsync();

                validShipmentData = SortOutShipmentData(shipment, filePath)
                    .ToList();
            }
            catch (Exception e)
            {
                var errorMessage = e.InnerException != null
                    ? $"{e.Message} {e.InnerException.Message}"
                    : e.Message;

                await EventBus
                    .PublishAsync(new FileProcessExceptionEvent
                    {
                        Description = errorMessage,
                        CreatedAt = DateTime.Now,
                        FileLocation = filePath
                    });
            }

            return validShipmentData;
        }

Example row of my csv file looks like this:我的 csv 文件的示例行如下所示:

... "06/08/2020";"ABCD";"1234"... ...“2020 年 6 月 8 日”;“ABCD”;“1234”...

The problem is, that some files imported to my api, contain no quotes.问题是,导入到我的 api 的某些文件不包含引号。 Exemplary row shown below:示例行如下所示:

... 06/08/2020;ABCD;1234... ... 2020 年 6 月 8 日;ABCD;1234...

When such a file comes to my application, an exception is being thrown, while trying to get records from file.当这样的文件进入我的应用程序时,会抛出异常,同时尝试从文件中获取记录。 Quoted files are processed properly.引用的文件得到正确处理。 Exception contains the following message:异常包含以下消息:

"No members are mapped for type '...'" “没有为类型'...'映射任何成员”

Of course columns specified in header are compliant with ShipmentData definition.当然,header 中指定的列符合 ShipmentData 定义。 The only problem i've noticed are missing quotes around cells.我注意到的唯一问题是单元格周围缺少引号。 I would like to rewrite my method to be able to get records from both types of csv.我想重写我的方法,以便能够从两种类型的 csv 中获取记录。 How to achieve that using CsvHelper library??如何使用 CsvHelper 库来实现这一点? Thanks for any answers.感谢您的任何回答。

You may have to use Read() and loop through the rows, as described in this issue , example in this comment .您可能必须使用 Read() 并遍历行,如本期所述, 本评论中的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM