2013-03-21 51 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,,,, 

我请注意,最后一列中没有数据。这只是一个复制和粘贴错误或故意?

编辑:我用选项推断在,这就是为什么一些类型的声明丢失。

相关问题