我正在处理一段应用程序,需要分析由PostgreSql服务器生成的CSV日志。
- The basic utility after Parse the Log is to show contents in a DataGridView.
- There are other filter options like to view log contents for a particular range of Time for a Day.
然而,的主要问题是日志格式不可读
它首先用一个快速的CSV阅读器测试
然后,我们使用String.Split方法制作了一个自定义实用程序,通常使用Foreach循环遍历数组
一个样本日志数据线
2012-03-21 11:59:20.640 IST,"postgres"," stock_apals ",3276,"localhost:1639",4f697540.ccc,10,“空闲”,2012-03-21 11:59:20 IST,2/163,0,LOG,00000,语句:选择id,pdate,itemname,qty (按日期分列),id,,,"exec_simple_query,.\src\backend\tcop\postgres.c:900“
如您所见,日志中的列是逗号分隔的,但是单个值不包含在Quote中。
例如,第一,第四,第六。列
是否有一个实用程序或Regex可以查找格式错误的列并放置引号?
尤其是在性能方面,因为这些日志很长,而且几乎每小时都会生成新的日志。
我只想更新列并使用FastCSVReader来解析它。
谢谢你的建议和帮助
发布于 2012-03-23 07:33:24
我已经更新了我的csv解析器,所以它现在能够解析数据(至少在示例中提供了)。下面是示例控制台应用程序,它解析保存在multiline_quotes.txt文件中的数据。项目源代码可以找到这里 (您可以下载ZIP)。您需要Gorgon.Parsing或Gorgon.Parsing.Net35 (以防无法使用.NET 4.0)。
实际上,我能够使用快速CSV阅读器获得相同的结果。你一开始就用错了。
namespace So9817628
{
using System.Data;
using System.Text;
using Gorgon.Parsing.Csv;
class Program
{
static void Main(string[] args)
{
// prepare
CsvParserSettings s = new CsvParserSettings();
s.CodePage = Encoding.Default;
s.ContainsHeader = false;
s.SplitString = ",";
s.EscapeString = "\"\"";
s.ContainsQuotes = true;
s.ContainsMultilineValues = true;
// uncomment below if you don't want escape quotes ("") to be replaced with single quote
//s.ReplaceEscapeString = false;
CsvParser parser = new CsvParser(s);
DataTable dt = parser.ParseToDataTableSequential("multiline_quotes.txt");
dt.WriteXml("parsed.xml");
}
}
}https://stackoverflow.com/questions/9817628
复制相似问题