2013-03-21 115 views
-1

我的文字數據文件是這樣的:轉換文本數據文件爲CSV格式

{1000}xxx{1200}xxx{3000}xxxxxx{5000} 
{1000}xx{1500}xxxxxx{4000}xx{6000} 
{1000}xxxx{1600}xxx{3000}xxx{6000} 
... 

我需要這個數據文件轉換爲csv文件或Excel文件來分析。我試過Excel或其他轉換軟件。但它不起作用。

我可以使用VB來做到這一點嗎?我很久沒有使用VB了(超過10年)。

對不起。我沒有說清楚。

花括號中的數字是字段名稱。每個記錄不具有相同的字段。轉換後應該是這樣的結果:

(header line) 1000 1200 1500 1600 3000 4000 5000 6000 
(record line) xxx xxx   xxx  xxx 
     .  xxx  xxx   xxx  xxx 
     .  xxx    xxx xxx   xxx 

我們有文本數據文件每天(10 - 20條)。雖然數據不是很大,但如果我們可以轉換成csv文件,我們不需要重新輸入excel文件。這可以幫助我們很多時間。

+0

結果應該是什麼樣子? – TAS 2013-03-21 17:36:45

回答

0

你幾乎可以肯定地使用一種編程語言(如VB)來做這個改變。我不確定你需要這樣做。

如果您試圖編寫一個程序來反覆轉換相同類型的文件,那麼在VB.net中構建程序可能是有意義的。

僅供參考,它很難幫助您進一步瞭解您需要做的事情嗎?例如,文件大小,你需要多長時間一次,目標格式是多少,等等......

......但我提供的答案確實回答了你問的問題! ...我正在尋求代表處點;)

+0

這裏的聲譽點通常是爲解決問題而頒發的。如果問題沒有明確說明,請嘗試通過對OP問題的評論來發現問題。 – Neolisk 2013-03-21 19:39:51

+0

感謝您的回覆。我們每天都這樣做。儘管數據量不大。每天有10-20筆交易。大括號中的數字實際上是字段名稱。每個記錄都沒有相同的字段。我們試圖將此文本數據文件轉換爲csv或excel文件。 – user2196273 2013-03-22 00:26:29

+0

Neolisk,我想要得到的一件事是能夠發表評論:(。 – Doug 2013-03-28 00:36:55

0

在你的數據是如何構成的解釋光:

Imports System.IO 
Imports System.Text 
Imports System.Text.RegularExpressions 

Module Module1 

    Class Cell 
     Property ColumnName As String 
     Property Value As String 

     ' To help with debugging/general usage 
     Public Overrides Function ToString() As String 
      Return String.Format("Col: {0} Val: {1}", ColumnName, Value) 
     End Function 
    End Class 

    Dim table As New List(Of List(Of Cell)) 

    Sub Main() 
     Dim src As String = "C:\temp\sampledata.txt" 
     Dim dest = "C:\temp\sampledata.csv" 

     Dim colNames As New List(Of String) 

     ' This regex will look for zero or more characters ".*" surrounded by braces "\{ \}" and 
     ' collect the zero or more characters in a group "()". The "?" makes it non-greedy. 
     ' The second capture group "()" gets all the characters up to but not including 
     ' the next "\{" (if it is present). 
     Dim cellSelector = New Regex("\{(.*?)\}([^\{]*)") 

     ' Read in the cells and record the column names. 
     Using inFile = New StreamReader(src) 
      While Not inFile.EndOfStream 
       Dim line = inFile.ReadLine 
       Dim rowContent As New List(Of Cell) 
       For Each m As Match In cellSelector.Matches(line) 
        rowContent.Add(New Cell With {.ColumnName = m.Groups(1).Value, .Value = m.Groups(2).Value}) 
        If Not colNames.Contains(m.Groups(1).Value) Then 
         colNames.Add(m.Groups(1).Value) 
        End If 
       Next 
       table.Add(rowContent.OrderBy(Function(c) c.ColumnName).ToList) 
      End While 
     End Using 

     colNames.Sort() 

     ' add the header row of the column names 
     Dim sb As New StringBuilder(String.Join(",", colNames) & vbCrLf) 

     ' output the data in csv format 
     For Each r In table 

      Dim col = 0 
      Dim cellNo = 0 

      While cellNo < r.Count AndAlso col < colNames.Count 
       ' If this row has a cell with the appropriate column name then 
       ' add the value to the output. 
       If r(cellNo).ColumnName = colNames(col) Then 
        sb.Append(r(cellNo).Value) 
        cellNo += 1 
       End If 

       ' add a separator if is not the last item in the row 
       If col < colNames.Count - 1 Then 
        sb.Append(","c) 
       End If 

       col += 1 

      End While 

      sb.AppendLine() 

     Next 

     File.WriteAllText(dest, sb.ToString) 

    End Sub 

End Module 

從你的樣本數據,輸出

1000,1200,1500,1600,3000,4000,5000,6000 
xxx,xxx,,,xxxxxx,,, 
xx,,xxxxxx,,,xx,,, 
xxxx,,,xxx,xxx,,,, 

我請注意,最後一列中沒有數據。這只是一個複製和粘貼錯誤或故意?

編輯:我用選項推斷在,這就是爲什麼一些類型的聲明丟失。