简体   繁体   English

使用FileHelpers; 如何解析此CSV类型

[英]Using FileHelpers; how to parse this CSV type

Having a few problems trying to parse a CSV in the following format using the FileHelpers library. 尝试使用FileHelpers库解析以下格式的CSV时遇到一些问题。 It's confusing me slightly because the field delimiter appears to be a space, but the fields themselves are sometimes quoted with quotation marks, and other times by square brackets. 这让我有些困惑,因为字段定界符似乎是一个空格,但是字段本身有时用引号引起来,而有时用方括号引起。 I'm trying to produce a RecordClass capable of parsing this. 我试图产生一个能够解析此的RecordClass。

Here's a sample from the CSV: 这是CSV中的示例:

xxx.xxx.xxx.xxx - - [14/Jun/2008:18:04:17 +0000] "GET http://www.some_url.com HTTP/1.1" 200 73662339 "-" "iTunes/7.6.2 (Macintosh; N; Intel)"

It's an extract from an HTTP log we receive from one of our bandwidth providers. 这是我们从一个带宽提供商收到的HTTP日志的摘录。

The obvious statement is "then it isn't CSV"... 明显的说法是“那么它不是CSV” ...

I'd be tempted to use a quick regex to munge the date into the same escaping as everything else... on a line-by-line basis, something like: 我很想使用快速的正则表达式将日期与其他所有内容转义为相同的转义...逐行,例如:

string t = Regex.Replace(s, @"\[([^\]]*)\]", @"""$1""")

Then you should be able to use a standard parser using space as a delimiter (respecting quotes). 然后,您应该能够使用使用空格作为定界符的标准解析器(使用引号)。

While I thank Marc Gravell and Jon Skeet for their input, my question was how to go about parsing a file containing lines in the format described using the FileHelpers library (albeit, I worded it badly to begin with, describing 'CSV' when in fact, it isn't). 尽管我感谢Marc Gravell和Jon Skeet的输入,但我的问题是如何解析包含使用FileHelpers库描述的格式的行的文件(尽管我的措辞很糟糕,实际上描述了“ CSV” ,不是)。

I have now found a way to do just this. 我现在找到了一种方法来做到这一点。 It's not particularly the most elegant method, however, it gets the job done. 它并不是最优雅的方法,但是可以完成工作。 In an ideal world, I wouldn't be using FileHelpers in this particular implementation ;) 在理想的世界中,我不会在此特定实现中使用FileHelpers;)

For those who are interested, the solution is to create a FileRecord class as follows: 对于那些感兴趣的人,解决方案是创建一个FileRecord类,如下所示:

[DelimitedRecord(" ")]
public sealed class HTTPRecord
{

public String IP;

// Fields with prefix 'x' are useless to me... we omit those in processing later
public String x1;
[FieldDelimiter("[")]
public String x2;


[FieldDelimiter("]")]
public String Timestamp;

[FieldDelimiter("\"")]
public String x3;

public String Method;
public String URL;

[FieldDelimiter("\"")]
public String Type;

[FieldIgnored()]
public String x4;

[FieldDelimiter(" ")]
public String x5;

public int HTTPStatusCode;

public long Bytes;

[FieldQuoted()] 
public String Referer;

[FieldQuoted()] 
public String UserAgent;
}

In what way is that CSV? CSV是哪种方式? It looks like it's just a particular log file format which should be fairly easily parsed, but not by a CSV parser. 看起来,这只是一种特定的日志文件格式,应该相当容易地进行解析,而不是通过CSV解析器进行解析。 In particular, you may well find that a regex works perfectly well. 特别是,您可能会发现正则表达式可以很好地工作。 (You'd need to check what would happen to quotes in the user agent etc.) (您需要检查用户代理等中的报价会发生什么情况。)

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

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