当前位置: 首页 > news >正文

C#实现读写CSV文件的方法详解

目录
  • CSV文件标准
    • 文件示例
    • RFC 4180
    • 简化标准
  • 读写CSV文件
    • 使用CsvHelper
    • 使用自定义方法
  • 总结

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件的解析。本文会介绍CsvHelperTextFieldParser正则表达式三种解析CSV文件的方法,顺带也会介绍一下CSV文件的写方法。

CSV文件标准

在介绍CSV文件的读写方法前,我们需要了解一下CSV文件的格式。

文件示例

一个简单的CSV文件:

?

1

2

3

Test1,Test2,Test3,Test4,Test5,Test6

str1,str2,str3,str4,str5,str6

str1,str2,str3,str4,str5,str6

一个不简单的CSV文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

"Test1

"",""","Test2

"",""","Test3

"",""","Test4

"",""","Test5

"",""","Test6

"","""

" 中文,D23 ","3DFD4234""""""1232""1S2","ASD1"",""23,,,,213

23F32","

",,asd

" 中文,D23 ","3DFD4234""""""1232""1S2","ASD1"",""23,,,,213

23F32","

",,asd

你没看错,上面两个都是CSV文件,都只有3行CSV数据。第二个文件多看一眼都是精神污染,但项目中无法避免会出现这种文件。

RFC 4180

CSV文件没有官方的标准,但一般项目都会遵守 RFC 4180 标准。这是一个非官方的标准,内容如下:

Each record is located on a separate line, delimited by a line break (CRLF).

The last record in the file may or may not have an ending line break.

There maybe an optional header line appearing as the first line of the file with the same format as normal record lines. This header will contain names corresponding to the fields in the file and should contain the same number of fields as the records in the rest of the file (the presence or absence of the header line should be indicated via the optional "header" parameter of this MIME type).

Within the header and each record, there may be one or more fields, separated by commas. Each line should contain the same number of fields throughout the file. Spaces are considered part of a field and should not be ignored. The last field in the record must not be followed by a comma.

Each field may or may not be enclosed in double quotes (however some programs, such as Microsoft Excel, do not use double quotes at all). If fields are not enclosed with double quotes, then double quotes may not appear inside the fields.

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.

If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote.

翻译一下:

  • 每条记录位于单独的行上,由换行符 (CRLF) 分隔。
  • 文件中的最后一条记录可能有也可能没有结束换行符。
  • 可能有一个可选的标题行出现在文件的第一行,格式与普通记录行相同。此标题将包含与文件中的字段对应的名称,并且应包含与文件其余部分中的记录相同数量的字段(标题行的存在或不存在应通过此 MIME 类型的可选“标头”参数指示)。
  • 在标题和每条记录中,可能有一个或多个字段,以逗号分隔。在整个文件中,每行应包含相同数量的字段。空格被视为字段的一部分,不应忽略。记录中的最后一个字段后面不能有逗号。
  • 每个字段可以用双引号括起来,也可以不用双引号(但是某些程序,例如 Microsoft Excel,根本不使用双引号)。如果字段没有用双引号括起来,那么双引号可能不会出现在字段内。
  • 包含换行符 (CRLF)、双引号和逗号的字段应该用双引号括起来。
  • 如果使用双引号将字段括起来,则出现在字段中的双引号必须在其前面加上另一个双引号。

简化标准

上面的标准可能比较拗口,我们对它进行一些简化。要注意一下,简化不是简单的删减规则,而是将类似的类似进行合并便于理解。
后面的代码也会使用简化标准,简化标准如下:

  • 每条记录位于单独的行上,由换行符 (CRLF) 分隔。
  • 注:此处的行不是普通文本意义上的行,是指符合CSV文件格式的一条记录(后面简称为CSV行),在文本上可能占据多行。
  • 文件中的最后一条记录需有结束换行符,文件的第一行为标题行(标题行包含字段对应的名称,标题数与记录的字段数相同)。
  • 注:原标准中可有可无的选项统一规定为必须有,方便后期的解析,而且没有标题行让别人怎么看数据。
  • 在标题和每条记录中,可能有一个或多个字段,以逗号分隔。在整个文件中,每行应包含相同数量的字段空格被视为字段的一部分,不应忽略。记录中的最后一个字段后面不能有逗号
  • 注:此标准未做简化,虽然也有其它标准使用空格、制表符等做分割的,但不使用逗号分割的文件还叫逗号分隔值文件吗。
  • 每个字段都用双引号括起来,出现在字段中的双引号必须在其前面加上另一个双引号
  • 注:原标准有必须使用双引号和可选双引号的情况,那全部使用双引号肯定不会出错。*

读写CSV文件

在正式读写CSV文件前,我们需要先定义一个用于测试的Test类。代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

class Test

{

    public string Test1{get;set;}

    public string Test2 { get; set; }

    public string Test3 { get; set; }

    public string Test4 { get; set; }

    public string Test5 { get; set; }

    public string Test6 { get; set; }

    //Parse方法会在自定义读写CSV文件时用到

    public static Test Parse (string[]fields )

    {

        try

        {

            Test ret = new Test();

            ret.Test1 = fields[0];

            ret.Test2 = fields[1];

            ret.Test3 = fields[2];

            ret.Test4 = fields[3];

            ret.Test5 = fields[4];

            ret.Test6 = fields[5];

            return ret;

        }

        catch (Exception)

        {

            //做一些异常处理,写日志之类的

            return null;

        }

    }

}

生成一些测试数据,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

static void Main(string[] args)

{

    //文件保存路径

    string path = "tset.csv";

    //清理之前的测试文件

    File.Delete("tset.csv");

       

    Test test = new Test();

    test.Test1 = " 中文,D23 ";

    test.Test2 = "3DFD4234\"\"\"1232\"1S2";

    test.Test3 = "ASD1\",\"23,,,,213\r23F32";

    test.Test4 = "\r";

    test.Test5 = string.Empty;

    test.Test6 = "asd";

    //测试数据

    var records = new List<Test> { test, test };

    //写CSV文件

    /*

    *直接把后面的写CSV文件代码复制到此处

    */

    //读CSV文件

     /*

    *直接把后面的读CSV文件代码复制到此处

    */

    

    Console.ReadLine();

}

使用CsvHelper

CsvHelper 是用于读取和写入 CSV 文件的库,支持自定义类对象的读写。

github上标星最高的CSV文件读写C#库,使用MS-PL、Apache 2.0开源协议。

使用NuGet下载CsvHelper,读写CSV文件的代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

//写CSV文件

using (var writer = new StreamWriter(path))

using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))

{

    csv.WriteRecords(records);

}

using (var writer = new StreamWriter(path,true))

using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))

{

    //追加

    foreach (var record in records)

    {

        csv.WriteRecord(record);

    }

}

//读CSV文件

using (var reader = new StreamReader(path))

using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))

{

    records = csv.GetRecords<Test>().ToList();

    //逐行读取

    //records.Add(csv.GetRecord<Test>());

}

如果你只想要拿来就能用的库,那文章基本上到这里就结束了。

使用自定义方法

为了与CsvHelper区分,新建一个CsvFile类存放自定义读写CSV文件的代码,最后会提供类的完整源码。CsvFile类定义如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/// <summary>

/// CSV文件读写工具类

/// </summary>

public class CsvFile

{

    #region 写CSV文件

    //具体代码...

    #endregion

    #region 读CSV文件(使用TextFieldParser)

    //具体代码...

    #endregion

    #region 读CSV文件(使用正则表达式)

    //具体代码...

    #endregion

}

基于简化标准的写CSV文件

根据简化标准(具体标准内容见前文),写CSV文件代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

#region 写CSV文件

//字段数组转为CSV记录行

private static string FieldsToLine(IEnumerable<string> fields)

{

    if (fields == null) return string.Empty;

    fields = fields.Select(field =>

    {

        if (field == null) field = string.Empty;

        //简化标准,所有字段都加双引号

        field = string.Format("\"{0}\"", field.Replace("\"", "\"\""));

        //不简化标准

        //field = field.Replace("\"", "\"\"");

        //if (field.IndexOfAny(new char[] { ',', '"', ' ', '\r' }) != -1)

        //{

        //    field = string.Format("\"{0}\"", field);

        //}

        return field;

    });

    string line = string.Format("{0}{1}", string.Join(",", fields), Environment.NewLine);

    return line;

}

//默认的字段转换方法

private static IEnumerable<string> GetObjFields<T>(T obj, bool isTitle) where T : class

{

    IEnumerable<string> fields;

    if (isTitle)

    {

        fields = obj.GetType().GetProperties().Select(pro => pro.Name);

    }

    else

    {

        fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());

    }

    return fields;

}

/// <summary>

/// 写CSV文件,默认第一行为标题

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="list">数据列表</param>

/// <param name="path">文件路径</param>

/// <param name="append">追加记录</param>

/// <param name="func">字段转换方法</param>

/// <param name="defaultEncoding"></param>

public static void Write<T>(List<T> list, string path,bool append=true, Func<T, bool, IEnumerable<string>> func = null, Encoding defaultEncoding = null) where T : class

{

    if (list == null || list.Count == 0) return;

    if (defaultEncoding == null)

    {

        defaultEncoding = Encoding.UTF8;

    }

    if (func == null)

    {

        func = GetObjFields;

    }

    if (!File.Exists(path)|| !append)

    {

        var fields = func(list[0], true);

        string title = FieldsToLine(fields);

        File.WriteAllText(path, title, defaultEncoding);

    }

    using (StreamWriter sw = new StreamWriter(path, true, defaultEncoding))

    {

        list.ForEach(obj =>

        {

            var fields = func(obj, false);

            string line = FieldsToLine(fields);

            sw.Write(line);

        });

    }

}

#endregion

使用时,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

//写CSV文件

//使用自定义的字段转换方法,也是文章开头复杂CSV文件使用字段转换方法

CsvFile.Write(records, path, true, new Func<Test, bool, IEnumerable<string>>((obj, isTitle) =>

{

    IEnumerable<string> fields;

    if (isTitle)

    {

        fields = obj.GetType().GetProperties().Select(pro => pro.Name + Environment.NewLine + "\",\"");

    }

    else

    {

        fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());

    }

    return fields;

}));

//使用默认的字段转换方法

//CsvFile.Write(records, path);

你也可以使用默认的字段转换方法,代码如下:

?

1

CsvFile.Save(records, path);

使用TextFieldParser解析CSV文件

TextFieldParser是VB中解析CSV文件的类,C#虽然没有类似功能的类,不过可以调用VB的TextFieldParser来实现功能。

TextFieldParser解析CSV文件的代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

#region 读CSV文件(使用TextFieldParser)

/// <summary>

/// 读CSV文件,默认第一行为标题

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="path">文件路径</param>

/// <param name="func">字段解析规则</param>

/// <param name="defaultEncoding">文件编码</param>

/// <returns></returns>

public static List<T> Read<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class

{

    if (defaultEncoding == null)

    {

        defaultEncoding = Encoding.UTF8;

    }

    List<T> list = new List<T>();

    using (TextFieldParser parser = new TextFieldParser(path, defaultEncoding))

    {

        parser.TextFieldType = FieldType.Delimited;

        //设定逗号分隔符

        parser.SetDelimiters(",");

        //设定不忽略字段前后的空格

        parser.TrimWhiteSpace = false;

        bool isLine = false;

        while (!parser.EndOfData)

        {

            string[] fields = parser.ReadFields();

            if (isLine)

            {

                var obj = func(fields);

                if (obj != null) list.Add(obj);

            }

            else

            {

                //忽略标题行业

                isLine = true;

            }

        }

    }

    return list;

}

#endregion

使用时,代码如下:

?

1

2

//读CSV文件

records = CsvFile.Read(path, Test.Parse);

使用正则表达式解析CSV文件

如果你有一个问题,想用正则表达式来解决,那么你就有两个问题了。

正则表达式有一定的学习门槛,而且学习后不经常使用就会忘记。正则表达式解决的大多数是一些不易变更需求的问题,这就导致一个稳定可用的正则表达式可以传好几代。

本节的正则表达式来自 《精通正则表达式(第3版)》 第6章 打造高效正则表达式——简单的消除循环的例子,有兴趣的可以去了解一下,表达式说明如下:

注:这本书最终版的解析CSV文件的正则表达式是Jave版的使用占有优先量词取代固化分组的版本,也是百度上经常见到的版本。不过占有优先量词在C#中有点问题,本人能力有限解决不了,所以使用了上图的版本。不过,这两版正则表达式性能上没有差异。

正则表达式解析CSV文件代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

#region 读CSV文件(使用正则表达式)

/// <summary>

/// 读CSV文件,默认第一行为标题

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="path">文件路径</param>

/// <param name="func">字段解析规则</param>

/// <param name="defaultEncoding">文件编码</param>

/// <returns></returns>

public static List<T> Read_Regex<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class

{

    List<T> list = new List<T>();

    StringBuilder sbr = new StringBuilder(100);

    Regex lineReg = new Regex("\"");

    Regex fieldReg = new Regex("\\G(?:^|,)(?:\"((?>[^\"]*)(?>\"\"[^\"]*)*)\"|([^\",]*))");

    Regex quotesReg = new Regex("\"\"");

    bool isLine = false;

    string line = string.Empty;

    using (StreamReader sr = new StreamReader(path))

    {

        while (null != (line = ReadLine(sr)))

        {

            sbr.Append(line);

            string str = sbr.ToString();

            //一个完整的CSV记录行,它的双引号一定是偶数

            if (lineReg.Matches(sbr.ToString()).Count % 2 == 0)

            {

                if (isLine)

                {

                    var fields = ParseCsvLine(sbr.ToString(), fieldReg, quotesReg).ToArray();

                    var obj = func(fields.ToArray());

                    if (obj != null) list.Add(obj);

                }

                else

                {

                    //忽略标题行业

                    isLine = true;

                }

                sbr.Clear();

            }

            else

            {

                sbr.Append(Environment.NewLine);

            }                  

        }

    }

    if (sbr.Length > 0)

    {

        //有解析失败的字符串,报错或忽略

    }

    return list;

}

//重写ReadLine方法,只有\r\n才是正确的一行

private static string ReadLine(StreamReader sr)

{

    StringBuilder sbr = new StringBuilder();

    char c;

    int cInt;

    while (-1 != (cInt =sr.Read()))

    {

        c = (char)cInt;

        if (c == '\n' && sbr.Length > 0 && sbr[sbr.Length - 1] == '\r')

        {

            sbr.Remove(sbr.Length - 1, 1);

            return sbr.ToString();

        }

        else

        {

            sbr.Append(c);

        }

    }

    return sbr.Length>0?sbr.ToString():null;

}

private static List<string> ParseCsvLine(string line, Regex fieldReg, Regex quotesReg)

{

    var fieldMath = fieldReg.Match(line);

    List<string> fields = new List<string>();

    while (fieldMath.Success)

    {

        string field;

        if (fieldMath.Groups[1].Success)

        {

            field = quotesReg.Replace(fieldMath.Groups[1].Value, "\"");

        }

        else

        {

            field = fieldMath.Groups[2].Value;

        }

        fields.Add(field);

        fieldMath = fieldMath.NextMatch();

    }

    return fields;

}

#endregion

使用时代码如下:

?

1

2

//读CSV文件

records = CsvFile.Read_Regex(path, Test.Parse);

目前还未发现正则表达式解析有什么bug,不过还是不建议使用。

完整的CsvFile工具类

完整的CsvFile类代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

using Microsoft.VisualBasic.FileIO;

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

namespace ConsoleApp4

{

    /// <summary>

    /// CSV文件读写工具类

    /// </summary>

    public class CsvFile

    {

        #region 写CSV文件

        //字段数组转为CSV记录行

        private static string FieldsToLine(IEnumerable<string> fields)

        {

            if (fields == null) return string.Empty;

            fields = fields.Select(field =>

            {

                if (field == null) field = string.Empty;

                //所有字段都加双引号

                field = string.Format("\"{0}\"", field.Replace("\"", "\"\""));

                //不简化

                //field = field.Replace("\"", "\"\"");

                //if (field.IndexOfAny(new char[] { ',', '"', ' ', '\r' }) != -1)

                //{

                //    field = string.Format("\"{0}\"", field);

                //}

                return field;

            });

            string line = string.Format("{0}{1}", string.Join(",", fields), Environment.NewLine);

            return line;

        }

        //默认的字段转换方法

        private static IEnumerable<string> GetObjFields<T>(T obj, bool isTitle) where T : class

        {

            IEnumerable<string> fields;

            if (isTitle)

            {

                fields = obj.GetType().GetProperties().Select(pro => pro.Name);

            }

            else

            {

                fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());

            }

            return fields;

        }

        /// <summary>

        /// 写CSV文件,默认第一行为标题

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="list">数据列表</param>

        /// <param name="path">文件路径</param>

        /// <param name="append">追加记录</param>

        /// <param name="func">字段转换方法</param>

        /// <param name="defaultEncoding"></param>

        public static void Write<T>(List<T> list, string path,bool append=true, Func<T, bool, IEnumerable<string>> func = null, Encoding defaultEncoding = null) where T : class

        {

            if (list == null || list.Count == 0) return;

            if (defaultEncoding == null)

            {

                defaultEncoding = Encoding.UTF8;

            }

            if (func == null)

            {

                func = GetObjFields;

            }

            if (!File.Exists(path)|| !append)

            {

                var fields = func(list[0], true);

                string title = FieldsToLine(fields);

                File.WriteAllText(path, title, defaultEncoding);

            }

            using (StreamWriter sw = new StreamWriter(path, true, defaultEncoding))

            {

                list.ForEach(obj =>

                {

                    var fields = func(obj, false);

                    string line = FieldsToLine(fields);

                    sw.Write(line);

                });

            }

        }

        #endregion

        #region 读CSV文件(使用TextFieldParser)

        /// <summary>

        /// 读CSV文件,默认第一行为标题

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="path">文件路径</param>

        /// <param name="func">字段解析规则</param>

        /// <param name="defaultEncoding">文件编码</param>

        /// <returns></returns>

        public static List<T> Read<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class

        {

            if (defaultEncoding == null)

            {

                defaultEncoding = Encoding.UTF8;

            }

            List<T> list = new List<T>();

            using (TextFieldParser parser = new TextFieldParser(path, defaultEncoding))

            {

                parser.TextFieldType = FieldType.Delimited;

                //设定逗号分隔符

                parser.SetDelimiters(",");

                //设定不忽略字段前后的空格

                parser.TrimWhiteSpace = false;

                bool isLine = false;

                while (!parser.EndOfData)

                {

                    string[] fields = parser.ReadFields();

                    if (isLine)

                    {

                        var obj = func(fields);

                        if (obj != null) list.Add(obj);

                    }

                    else

                    {

                        //忽略标题行业

                        isLine = true;

                    }

                }

            }

            return list;

        }

        #endregion

        #region 读CSV文件(使用正则表达式)

        /// <summary>

        /// 读CSV文件,默认第一行为标题

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="path">文件路径</param>

        /// <param name="func">字段解析规则</param>

        /// <param name="defaultEncoding">文件编码</param>

        /// <returns></returns>

        public static List<T> Read_Regex<T>(string path, Func<string[], T> func, Encoding defaultEncoding = null) where T : class

        {

            List<T> list = new List<T>();

            StringBuilder sbr = new StringBuilder(100);

            Regex lineReg = new Regex("\"");

            Regex fieldReg = new Regex("\\G(?:^|,)(?:\"((?>[^\"]*)(?>\"\"[^\"]*)*)\"|([^\",]*))");

            Regex quotesReg = new Regex("\"\"");

            bool isLine = false;

            string line = string.Empty;

            using (StreamReader sr = new StreamReader(path))

            {

                while (null != (line = ReadLine(sr)))

                {

                    sbr.Append(line);

                    string str = sbr.ToString();

                    //一个完整的CSV记录行,它的双引号一定是偶数

                    if (lineReg.Matches(sbr.ToString()).Count % 2 == 0)

                    {

                        if (isLine)

                        {

                            var fields = ParseCsvLine(sbr.ToString(), fieldReg, quotesReg).ToArray();

                            var obj = func(fields.ToArray());

                            if (obj != null) list.Add(obj);

                        }

                        else

                        {

                            //忽略标题行业

                            isLine = true;

                        }

                        sbr.Clear();

                    }

                    else

                    {

                        sbr.Append(Environment.NewLine);

                    }                  

                }

            }

            if (sbr.Length > 0)

            {

                //有解析失败的字符串,报错或忽略

            }

            return list;

        }

        //重写ReadLine方法,只有\r\n才是正确的一行

        private static string ReadLine(StreamReader sr)

        {

            StringBuilder sbr = new StringBuilder();

            char c;

            int cInt;

            while (-1 != (cInt =sr.Read()))

            {

                c = (char)cInt;

                if (c == '\n' && sbr.Length > 0 && sbr[sbr.Length - 1] == '\r')

                {

                    sbr.Remove(sbr.Length - 1, 1);

                    return sbr.ToString();

                }

                else

                {

                    sbr.Append(c);

                }

            }

            return sbr.Length>0?sbr.ToString():null;

        }

        

        private static List<string> ParseCsvLine(string line, Regex fieldReg, Regex quotesReg)

        {

            var fieldMath = fieldReg.Match(line);

            List<string> fields = new List<string>();

            while (fieldMath.Success)

            {

                string field;

                if (fieldMath.Groups[1].Success)

                {

                    field = quotesReg.Replace(fieldMath.Groups[1].Value, "\"");

                }

                else

                {

                    field = fieldMath.Groups[2].Value;

                }

                fields.Add(field);

                fieldMath = fieldMath.NextMatch();

            }

            return fields;

        }

        #endregion

    }

}

使用方法如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

//写CSV文件

CsvFile.Write(records, path, true, new Func<Test, bool, IEnumerable<string>>((obj, isTitle) =>

{

    IEnumerable<string> fields;

    if (isTitle)

    {

        fields = obj.GetType().GetProperties().Select(pro => pro.Name + Environment.NewLine + "\",\"");

    }

    else

    {

        fields = obj.GetType().GetProperties().Select(pro => pro.GetValue(obj)?.ToString());

    }

    return fields;

}));

//读CSV文件

records = CsvFile.Read(path, Test.Parse);

//读CSV文件

records = CsvFile.Read_Regex(path, Test.Parse);

相关文章:

C#实现读写CSV文件的方法详解

目录 CSV文件标准 文件示例RFC 4180简化标准读写CSV文件 使用CsvHelper使用自定义方法总结 项目中经常遇到CSV文件的读写需求&#xff0c;其中的难点主要是CSV文件的解析。本文会介绍CsvHelper、TextFieldParser、正则表达式三种解析CSV文件的方法&#xff0c;顺带也会介绍一…...

04 http连接处理(上)

基础知识&#xff1a;epoll、http报文格式、状态码和有限状态机 代码&#xff1a;对服务端处理http请求的全部流程进行简要介绍&#xff0c;然后结合代码对http类及请求接收进行详细分析。 epoll epoll_create函数 #include <sys/epoll.h> int epoll_create(int size)…...

c++(强生成关键字+可变参数模板+emplace)[26]

强制生成 不生成 在C中&#xff0c;可以通过一些方式来控制编译器是否生成某些特殊成员函数&#xff08;如默认构造函数、拷贝构造函数、拷贝赋值运算符、析构函数等&#xff09;。 默认生成&#xff1a;如果你没有显式地定义这些特殊成员函数&#xff0c;编译器会自动生成它们…...

Mysql 数据库开发及企业级应用

文章目录 1、Mysql 数据库开发及企业级应用1.1、为什么要使用数据库1.1.1、数据库概念&#xff08;Database&#xff09;1.1.2、为什么需要数据库 1.2、程序员为什么要学习数据库1.3、数据库的选择1.3.1、主流数据库简介1.3.2、使用 MySQL 的优势1.3.3、版本选择 1.4、Windows …...

【数据结构】_6.队列

目录 1.概念 2.队列的使用 3.队列模拟实现 4.循环队列 5.双端队列 6.OJ题 6.1 用队列实现栈 6.2 用栈实现队列 1.概念 &#xff08;1&#xff09;队列是只允许在一端进行插入数据操作&#xff0c;在另一端进行删除数据操作的特殊线性表&#xff1b; &#xff08;2&am…...

7 网络通信(上)

文章目录 网络通信概述ip地址ip的作用ip地址的分类私有ip 掩码和广播地址 linux 命令&#xff08;ping ifconfig&#xff09;查看或配置网卡信息&#xff1a;ifconfig(widows 用ipconfig)测试远程主机连通性&#xff1a;ping路由查看 端口端口是怎样分配的知名端口动态端口 查看…...

MFC图表控件high-speed-charting的使用

high-speed-charting是MFC上的开源图表库,Teechart的替代品。 high-speed-charting的下载地址 https://www.codeproject.com/Articles/14075/High-speed-Charting-Control 特性 High-speed drawing (when axis is fixed) which allows fast plotting of dataUnlimited number …...

Unity中常用方法

1.基础 //初始化引入 [RequireComponent(typeof(BoxCollider2D))] [RequireComponent(typeof(Rigidbody2D))]//游戏帧率设置 60帧Application.targetFrameRate 60;//获取物体对象 //获取到当前物体(根据名称&#xff0c;也可以根据路径)GameObject go GameObject.Find("…...

【监控系统】可视化工具Grafana简介及容器化部署实战

1.什么是Grafana 官网地址&#xff1a;https://grafana.com/ Grafana用Go语言开发的开源数据可视化工具&#xff0c;可以做数据监控和数据统计&#xff0c;带有告警功能。支持快速灵活的客户端图表&#xff0c;面板插件有许多不同方式的可视化指标和日志&#xff0c;官方库中…...

VUE之VueRouter页面跳转

参考资料&#xff1a; 参考视频 参考demo及视频资料 VUE之基本部署及VScode常用插件 VUE之基本组成和使用 VUE之Bootstrap和Element-UI的使用 VUE之axios使用&#xff0c;跨域问题&#xff0c;拦截器添加Token Vue Router官网 Vue Router说明&#xff1a; 说明&#xf…...

【188】Java8利用AVL树实现Map

AVL树又被叫做平衡二叉搜索树、平衡二叉树。AVL是其发明者的首字母缩写。 这篇文章中&#xff0c;AVLTreeMap 类集成了 java.util.Map 接口&#xff0c;并利用 AVL 树结构实现了 Map 接口的所有方法。本文还给出了测试代码。 为什么要发明AVL树&#xff1f; 当我按照从小到大…...

[SQL挖掘机] - 右连接: right join

介绍: 右连接是一种多表连接方式&#xff0c;它以右侧的表为基础&#xff0c;并返回满足连接条件的匹配行以及右侧表中的所有行&#xff0c;即使左侧的表中没有匹配的行。右连接将右表的每一行与左表进行比较&#xff0c;并根据连接条件返回结果集。其实, 左连接和右连接原理一…...

bug篇之基于docker安装nacos(2.1.1)使用dubbo连接不上的问题

说明&#xff1a;首先我的nacos安装是2.1.1版本&#xff0c;请注意版本问题。另外启动时用dubbo的话必须先启动服务提供者再启动服务使用者&#xff0c;否则会报错&#xff0c;同时也必须开放三个端口&#xff1a;8848&#xff0c;9848&#xff0c;9849 java.lang.IllegalStat…...

【Python入门系列】第二十一篇:Python物联网和传感器应用

文章目录 前言一、Python在物联网和传感器应用中的优势二、连接传感器和设备三、读取传感器数据四、示例代码和讲解五、进一步处理和分析传感器数据六、更多应用示例1、温湿度监测系统2、智能家居系统 - 灯光控制 总结 前言 物联网和传感器在现代科技中扮演着重要的角色。物联…...

Python爬虫的urlib的学习(学习于b站尚硅谷)

目录 一、页面结构的介绍  1.学习目标  2.为什么要了解页面&#xff08;html&#xff09;  3. html中的标签&#xff08;仅介绍了含表格、无序列表、有序列表、超链接&#xff09;  4.本节的演示 二、Urllib  1.什么是互联网爬虫&#xff1f;  2.爬虫核心  3.爬虫…...

【MongoDB】--MongoDB聚合Aggregation

目录 一、前言二、聚合管道操作2.1、实际案例1(1)、案例--根据学生no&#xff0c;找到对应班级名称(2)、案例--这个班级有哪些学生和哪些老师在任课 2.2、实际案例2(1)、案例--主表和关联表都有条件限制&#xff0c;且分页返回 一、前言 聚合操作组值来自多个文档&#xff0c;…...

Hadoop学习指南:探索大数据时代的重要组成——Hadoop概述

前言 在当今大数据时代&#xff0c;处理海量数据成为了一项关键任务。Hadoop作为一种开源的分布式计算框架&#xff0c;为大规模数据处理和存储提供了强大的解决方案。本文将介绍Hadoop的组成和其在大数据处理中的重要作用&#xff0c;让我们一同踏上学习Hadoop的旅程。 Hado…...

Java实现简单小画板

Java制作简单画板&#xff0c;包括两个类&#xff0c;一个主要画板类Drawpad&#xff0c;一个画板监听器DrawListener类。 1、Drawpad类&#xff0c;包括画板&#xff0c;画板功能设计&#xff0c;保存图片等 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2…...

B078-项目实战--支付模块 领养订单支付流程

目录 支付模块需求分析表设计支付单表支付宝账号信息表-商家账号微信支付账号信息表-商家账号银行账号表-商家资金账号表支付流水表 流程分析支付基础模块继承加密算法沙箱环境准备支付宝支付-流程分析根据demo封装工具类导入依赖AlipayConfigAlipayInfoAlipayUtil 内网穿透 领…...

[css]margin-top不起作用问题(外边距合并)

在初学css时&#xff0c;会遇到突然间margin-top不起作用的情况。如下面&#xff1a; 情况一&#xff1a; 代码&#xff1a; <html> <head><style type"text/css"> * {margin:0;padding:0;border:0; }#outer {width:300px;height:300px;backgroun…...

Vue2基础八、插槽

零、文章目录 Vue2基础八、插槽 1、插槽 &#xff08;1&#xff09;默认插槽 作用&#xff1a;让组件内部的一些 结构 支持 自定义需求: 将需要多次显示的对话框, 封装成一个组件问题&#xff1a;组件的内容部分&#xff0c;不希望写死&#xff0c;希望能使用的时候自定义。…...

自然语言处理从入门到应用——LangChain:提示(Prompts)-[提示模板:连接到特征存储]

分类目录&#xff1a;《自然语言处理从入门到应用》总目录 特征存储是传统机器学习中的一个概念&#xff0c;它确保输入模型的数据是最新和相关的。在考虑将LLM应用程序投入生产时&#xff0c;这个概念非常重要。为了个性化LLM应用程序&#xff0c;我们可能希望将LLM与特定用户…...

jenkins自定义邮件发送人姓名

jenkins发送邮件的时候发送人姓名默认的&#xff0c;如果要自定义发件人姓名&#xff0c;只需要修改如下信息即可&#xff1a; 系统管理-system-Jenkins Location下的系统管理员邮件地址 格式为&#xff1a;自定义姓名<邮件地址>...

SolidWorks二次开发---简单的连接solidworks

创建一个.net Framework的应用&#xff0c;正常4.0以上就可以了。 打开nuget包管理 在里面搜索paine 在版中选择对应的solidworks年份开头的&#xff0c;进行安装。 安装完之后 : 同时选中下面两个dll,把嵌入操作类型改为false 然后在按钮的单击事件中输入: Connect.Crea…...

docker 安装 active Mq

在安装完Docker的机器上&#xff0c;安装activeMQ。 拉取镜像&#xff1a; docker pull webcenter/activemq 查看镜像&#xff1a; docker images Docker运行ActiveMQ镜像 docker run --name activemq -d -p 8161:8161 -p 61616:61616 --privilegedtrue --restartalways …...

【Linux】TCP协议

​&#x1f320; 作者&#xff1a;阿亮joy. &#x1f386;专栏&#xff1a;《学会Linux》 &#x1f387; 座右铭&#xff1a;每个优秀的人都有一段沉默的时光&#xff0c;那段时光是付出了很多努力却得不到结果的日子&#xff0c;我们把它叫做扎根 目录 &#x1f449;TCP协议&…...

DevOps系列文章之 自动化测试大全(单测和集成测试)

自动化测试业界主流工具 核心目标&#xff1a; 主要是功能测试和覆盖率测试 业界常用主流工具 GoogleTest GoogleTest是一个跨平台的(Liunx、Mac OS X、Windows 、Cygwin 、Windows CE and Symbian ) C单元测试框架&#xff0c;由google公司发布&#xff0c;为在不同平台上为编…...

Android启动速度优化

本节主要内容&#xff1a;了解APP启动流程、启动状态、查看启动时间、CPU Profile定位启动耗时代码、StrictMode严苛模式检测不合理写法、解决启动黑白屏问题。 一、APP启动流程 ①用户点击桌面App图标&#xff0c;Launcher进程采用Binder IPC向system_server进程发起startAc…...

linux 日志 系统安全日志 web日志

web日志 LINUX日志系统之WEB日志&#xff08;一&#xff09;_dracut.log_麻子来了的博客-CSDN博客 系统安全日志 Linux系统安全日志详解_sinolover的博客-CSDN博客 wtmp和utmp文件都是二进制文件&#xff0c;需使用who、w、users、last和ac来操作这两个文件。 who /var/lo…...

SpringBoot 整合 MongoDB 连接 阿里云MongoDB

注&#xff1a;spring-boot-starter-data-mongodb 2.7.5&#xff1b;jdk 1.8 阿里云MongoDB是副本集实例的 在网上查找了一番&#xff0c;大多数都是教连接本地mongodb或者linux上的mongodb 阿里云上有java版连接教程&#xff0c;但它不是SpringBoot方法配置的&#xff0c;是手…...