• 大小: 36KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: C#
  • 标签: csv  

资源简介

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

评论

共有 条评论