【问题标题】:Read file in CSV helper在 CSV 帮助程序中读取文件
【发布时间】:2019-10-23 11:00:05
【问题描述】:

尝试读取数据不是用逗号分隔,而是使用引号的文件:"

例如:

"num" "date" "callsite" "level" "thread"

这可以吗?

csvhelper 给出找不到标头的异常。

【问题讨论】:

  • 数据是制表符还是空格分隔?

标签: csvhelper


【解决方案1】:

如果你的数据是用制表符分隔的,那么你可以设置csv.Configuration.Delimiter = "\t";

public class Program
{
    public static void Main(string[] args)
    {

        using (MemoryStream stream = new MemoryStream())
        using (StreamWriter writer = new StreamWriter(stream))
        using (StreamReader reader = new StreamReader(stream))
        using (CsvReader csv = new CsvReader(reader))
        {
            writer.WriteLine("\"num\"\t\"date\"\t\"callsite\"\t\"level\"\t\"thread\"");
            writer.WriteLine("\"1\"\t\"1/1/2019\"\t\"callsite1\"\t\"high\"\t\"20\"");
            writer.Flush();
            stream.Position = 0;

            csv.Configuration.Delimiter = "\t";

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


        Console.ReadKey();
    }
}

public class Foo
{
    public int num { get; set; }
    public DateTime date { get; set; }
    public string callsite { get; set; }
    public string level { get; set; }
    public string thread { get; set; }
}

【讨论】:

  • 谢谢,看起来我在标题之间插入了制表符,将 \t 设置为分隔符解决了这个问题。
  • 很高兴为您提供帮助。我更改了答案以反映制表符分隔的文件。
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
相关资源
最近更新 更多