资源简介
C#解析csv,解决中文编码的一个代码例子
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.Regularexpressions;
namespace ask_query_parser
{
class Program
{
static void Main(string[] args)
{
string filename = @“C:\Documents and Settings\Cao Zhongyan\Desktop\ask_query_20190305001.csv“;
string outputfile = @“C:\Documents and Settings\Cao Zhongyan\Desktop\ask_stat.csv“;
var lines = EnumLines(File.ReadAllLines(filename));
var query = lines.Select(x => EnumCols(x).ToArray()).Where(x => x.Count() >= 4).Skip(1);
File.WriteAllText(outputfile “titleurldtaccepted\r\n“ Encoding.GetEncoding(“GB2312“));
var result = query.GroupBy(x => x[1]).Select(x => new { title = x.First()[0] url = x.Key acc = x.Any(y => y[3] == “是“) dt = x.First()[2] })
.Select(x => string.Format(“{0} {1} {2} {3}“ x.title.Replace(““ ““).Replace(“\““ ““).Replace(“\\“ ““) x.url x.dt x.acc));
File.AppendAllLines(outputfile result Encoding.GetEncoding(“GB2312“));
}
enum state
{
outquo
inquo
firstslash
}
static IEnumerable EnumCols(string line)
{
StringBuilder buf = new StringBuilder();
state st = state.outquo;
foreach (var item in line)
{
if (st == state.outquo)
{
if (item == ‘\“‘)
{
st = state.inquo;
buf.Clear();
}
}
else if (st == state.inquo)
{
if (item == ‘\\‘)
{
buf.Append(item);
st = state.firstslash;
}
if (item == ‘\“‘)
{
yield return buf.ToString();
st = state.outquo;
}
else
buf.Append(item);
}
else if (st == state.firstslash)
{
buf.Append(item);
st = state.inquo;
}
}
}
static string ProcStr(string p)
{
if (p.StartsWith(“\““))
p = p.Substring(2);
p = Regex.Replace(p @“(?!\\)\\\“““ “\““);
return p;
}
static IEnumerable EnumLines(IEnumerable lines)
{
bool inline = false;
string buf = ““;
foreach (var item in lines)
{
string sitem = Regex.Replace(item @“\\\“““ ““);
if (buf != ““) buf += “\r\n“;
buf += item;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 890 2019-03-13 13:01 ask_query_parser\ask_query_parser.sln
文件 16896 2019-03-13 14:25 ask_query_parser\ask_query_parser.suo
目录 0 2019-03-13 13:01 ask_query_parser\ask_query_parser\
文件 2453 2019-03-13 13:01 ask_query_parser\ask_query_parser\ask_query_parser.csproj
目录 0 2019-03-13 13:01 ask_query_parser\ask_query_parser\bin\
目录 0 2019-03-13 13:02 ask_query_parser\ask_query_parser\bin\Debug\
文件 13312 2019-03-13 14:23 ask_query_parser\ask_query_parser\bin\Debug\ask_query_parser.exe
文件 19968 2019-03-13 14:23 ask_query_parser\ask_query_parser\bin\Debug\ask_query_parser.pdb
文件 11600 2019-03-13 14:10 ask_query_parser\ask_query_parser\bin\Debug\ask_query_parser.vshost.exe
文件 490 2010-03-17 22:39 ask_query_parser\ask_query_parser\bin\Debug\ask_query_parser.vshost.exe.manifest
目录 0 2019-03-13 13:01 ask_query_parser\ask_query_parser\obj\
目录 0 2019-03-13 13:01 ask_query_parser\ask_query_parser\obj\x86\
目录 0 2019-03-13 14:23 ask_query_parser\ask_query_parser\obj\x86\Debug\
文件 752 2019-03-13 14:10 ask_query_parser\ask_query_parser\obj\x86\Debug\ask_query_parser.csproj.FileListAbsolute.txt
文件 13312 2019-03-13 14:23 ask_query_parser\ask_query_parser\obj\x86\Debug\ask_query_parser.exe
文件 19968 2019-03-13 14:23 ask_query_parser\ask_query_parser\obj\x86\Debug\ask_query_parser.pdb
文件 5935 2019-03-13 14:23 ask_query_parser\ask_query_parser\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
目录 0 2019-03-13 13:01 ask_query_parser\ask_query_parser\obj\x86\Debug\TempPE\
文件 3313 2019-03-13 14:23 ask_query_parser\ask_query_parser\Program.cs
目录 0 2019-03-13 13:01 ask_query_parser\ask_query_parser\Properties\
文件 1444 2019-03-13 13:01 ask_query_parser\ask_query_parser\Properties\AssemblyInfo.cs
- 上一篇:c#离散点生成TIN
- 下一篇:英语学习助手
相关资源
- 波斯顿房价数据集CSV格式
- flights.csv
- C#——导出数据到CSV
- 计算机视觉 顶会 CVPR_2019_全部论文开
- student.csv
- DayLoadSet.csv
- csv格式的鸢尾花数据集iris
- watermelon_3a.csv
- AirPassengers.csv
- 同义词词库.csv
- 秦皇岛煤价预测.csv
- watermelon3_0a.csv
- double_car.csv
- test5.csv
- german credit data
- sitka_weather_07-2014.csv
- spam_train.csv
- product_class(1).csv
- WA_Fn-UseC_-Telco-Customer-Churn.csv
- 全国地震CSV数据
- RFM练习数据.csv
- 风电场风速数据,风机出力数据.csv
- 数据分析练习 day_3 or 4 资源文件 sto
- california_housing_train.csv
- Kaggle-Titanic-train.csv泰坦尼克数据集
- sitka_weather_2014.csv
- 使用C#读取csv文件的201903
- POKEMON.csv
- svc_example.csv
- pima-indians-diabetes.csv
评论
共有 条评论