最佳方法将制表符分隔的CSV排序为对象?

时间:2016-07-17 22:15:49

标签: vb.net csv

我有一个看起来像这样的对象:

Public Class KeyWordObject
Private LocalSearches As String
Private AdvertiserCompetition As String
Private NumberOfWords As String

Public Sub New()
    ' Leave fields empty. 
End Sub

Public Sub New(ByVal LS As String, ByVal AC As String, ByVal NW As String)
    LocalSearches = LS
    AdvertiserCompetition = AC
    NumberOfWords = NW
End Sub

Public Property LocalSearchAmount As String
    Get
        Return LocalSearches
    End Get
    Set(ByVal value As String)
        LocalSearches = value
    End Set
End Property

Public Property AdvertiserCompetitionAmount As String
    Get
        Return AdvertiserCompetition
    End Get
    Set(ByVal value As String)
        AdvertiserCompetition = value
    End Set
End Property

Public Property WordCount As String
    Get
        Return NumberOfWords
    End Get
    Set(ByVal value As String)
        NumberOfWords = value
    End Set
End Property

结束班

我正在尝试使用以下标题解析CSV文件:

Ad group    Keyword     Avg. Monthly Searches (exact match only)    Competition    Suggested bid    Impr. share Organic impr. share

我想从关键字Avg获取数据。每月搜索和竞争列并将它们放入对象中。解析这些数据的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

我最终选择了CSVHelper,这是适合我的解决方案。感谢其他建议。

     Dim TEMPKEYWORDPATH = Directory.GetCurrentDirectory + "/" + "KeywordsTemp" + ".csv"
Public Function CSVtoObject(CsvPath As String)
    Dim csv = New CsvReader(File.OpenText(TEMPKEYWORDPATH))
    csv.Configuration.RegisterClassMap(Of MyCustomObjectMap)()
    csv.Configuration.WillThrowOnMissingField = False
    csv.Configuration.Delimiter = vbTab

    Dim ListOfKeywords = csv.GetRecords(Of KeyWordObject).ToList()
    Return ListOfKeywords
End Function

Public NotInheritable Class MyCustomObjectMap
    Inherits CsvClassMap(Of KeyWordObject)
    Public Sub New()
        Map(Function(m) m.Keyword).Index(1)
        Map(Function(m) m.LocalSearchAmount).Index(3)
        Map(Function(m) m.AdvertiserCompetitionAmount).Index(4)
        Map(Function(m) m.WordCount).Ignore()
    End Sub
End Class