CSV的新列/行数据 [英] New Column/Row Data for CSV

查看:90
本文介绍了CSV的新列/行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,需要在其中添加新列,即app_modulnameapp_module.statusapp_module.incrementapp_module.success.只需使用下面的代码行即可轻松完成此操作.

I have a CSV file where I need to add new columns namely app_modulname, app_module.status, app_module.increment, app_module.success. This could easily be done with this lines of codes below.

Dim csvData = File.ReadLines(filename).Select(Function(line, index) 
  If(index = 0, 
    line + ",app_module.name,app_module.status,app_module.increment,app_module.success",
    line + "," + moduleName & ",inactive," & index & ",")
  )

File.WriteAllLines(savePath, csvData)

对于app_module.increment列,行数据只是递增数字.问题在于上面的代码正在读取换行符.而且,我使用index作为增量数.还有其他获取行索引的方法吗?

For the app_module.increment column, the row data are just increment numbers. The problem is that the code above is reading the line breaks. And, I use index as the increment number. is there other way to get the index of row?

推荐答案

我从以下文本文件开始:

I started with the following text file:

One,"First record",Primary
Two,"Second
record",Secondary
Three,"Third record",Tertiary

我执行了以下代码:

Dim lines As New List(Of String)
Dim lineNumber = 1

Using parser As New TextFieldParser("TextFile1.txt") With {.Delimiters = {","}}
    Do Until parser.EndOfData
        Dim fields = parser.ReadFields()

        'Add quotes around the column that may be multiline.
        fields(1) = $"""{fields(1)}"""

        lines.Add(String.Join(",", fields) & "," & lineNumber)
        lineNumber += 1
    Loop
End Using

File.WriteAllLines("TextFile1.txt", lines)

我最终得到了这个文本文件:

I ended up with this text file:

One,"First record",Primary,1
Two,"Second
record",Secondary,2
Three,"Third record",Tertiary,3

这篇关于CSV的新列/行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆