文本文件(.txt)源记录如下:
03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
怎样用写代码的方法把上面的记录格式化为下面的格式:
03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
这样的格式呢
请教大家,祝大家新年快乐.

解决方案 »

  1.   

    你可以用Replace函数,将某字符或字符串代替成指定的字符,如Replace("abc d e f"," ","|")即返回值为"abc|d|e|f"
      

  2.   

    Dim i As Long
    Dim s1 As String
    Dim s2 As String
    s1 = "03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014"
    i = InStr(10, s1, " ")
    s2 = Right(s1, Len(s1) - i + 1)
    s1 = Left(s1, i - 1)
    s1 = s1 & Replace(s2, " ", "|")
    Debug.Print s1
      

  3.   

    谢谢大家的回复.但是:
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    只是文本文件(.txt)的一条记录,我想把整个文件里面的记录全部转换成我所要的格式.
    不知要怎样做?
    转换成的格式如下:
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
      

  4.   

    小马哥的代码如下:(感谢小马哥的帮助)
    Private Sub FormatTxt(strFromName As String, strToName As String)
    '开始格式化文本文件
        Dim strTmp As String
        Dim strArray() As String
        Dim a() As String
        Dim i As Integer
        Dim flag As Boolean
        Dim j As Integer
        Dim k As Integer
        Dim P As String
        Dim q As String
        'On Error Resume Next
        Open strFromName For Input As #1
        strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
        Close #1
        strArray = Split(strTmp, vbCrLf)
        For i = 0 To UBound(strArray)
            k = k + 1
            strTmp = ""
            If k = 1 Then
                Open strToName For Output As #1
                flag = True
                a() = Split(strArray(i), vbTab)
                For j = 0 To UBound(a)
                    strTmp = strTmp & "|" & a(j)
                Next j
                Print #1, Right(strTmp, Len(strTmp) - 1)
            Else
                strTmp = strArray(i)
                For j = 0 To Len(strTmp)
                    If Trim(Left(strTmp, 1) & "|") <> "|" Then
                        P = P & Left(strTmp, 1)
                    Else
                        If P <> "" Then
                            q = q & "|" & P
                            P = ""
                        End If
                    End If
                    If Len(strTmp) <= 1 Then
                        strTmp = strTmp
                    Else
                        strTmp = Right(strTmp, Len(strTmp) - 1)
                    End If
                Next j
                If Len(q) >= 1 Then Print #1, Right(q, Len(q) - 1)
                q = ""
            End If
        Next i
        If flag Then Close #1
    'err_exit:
        MsgBox Err.Description
        'MsgBox "要导入的文件未找到,请确认硬盘有此TXT文件"
    End Sub
    然后:
    Call WriteTempSchemia("zz.txt", "|") '空格吗?
    Call FormatTxt("c:\test.txt", "c:\zz.txt")
    其中:test.txt是源记录,zz.txt是格式后的记录文本
    但是转换后的格式为:
    03.12.23|00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014小马哥的代码我不太明白那位能帮忙修改一下.实现格式后的文本文件的记录为:
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
    谢谢大家.
      

  5.   

    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    这个怎么会那么复杂:意思是头一个空格不替换成“|”,而后面的空格替换成“|”
    str = "03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014"
    把小马哥的代码关键处即
               For j = 0 To UBound(a)
                    strTmp = strTmp & "|" & a(j)
                Next j
    改成如下就成:
                For j = 0 To UBound(a)
                    if j=0 then
                        strTmp = strTmp & " " & a(j)
                    else
                        strTmp = strTmp & "|" & a(j)
                    end if
                Next j
    改动后的全部代码如下Private Sub FormatTxt(strFromName As String, strToName As String)
    '开始格式化文本文件
        Dim strTmp As String
        Dim strArray() As String
        Dim a() As String
        Dim i As Integer
        Dim flag As Boolean
        Dim j As Integer
        Dim k As Integer
        Dim P As String
        Dim q As String
        'On Error Resume Next
        Open strFromName For Input As #1
        strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
        Close #1
        strArray = Split(strTmp, vbCrLf)
        For i = 0 To UBound(strArray)
            k = k + 1
            strTmp = ""
            If k = 1 Then
                Open strToName For Output As #1
                flag = True
                a() = Split(strArray(i), vbTab)
                For j = 0 To UBound(a)
                    if j=0 then
                        strTmp = strTmp & " " & a(j)
                    else
                        strTmp = strTmp & "|" & a(j)
                    end if
                Next j
                Print #1, Right(strTmp, Len(strTmp) - 1)
            Else
                strTmp = strArray(i)
                For j = 0 To Len(strTmp)
                    If Trim(Left(strTmp, 1) & "|") <> "|" Then
                        P = P & Left(strTmp, 1)
                    Else
                        If P <> "" Then
                            q = q & "|" & P
                            P = ""
                        End If
                    End If
                    If Len(strTmp) <= 1 Then
                        strTmp = strTmp
                    Else
                        strTmp = Right(strTmp, Len(strTmp) - 1)
                    End If
                Next j
                If Len(q) >= 1 Then Print #1, Right(q, Len(q) - 1)
                q = ""
            End If
        Next i
        If flag Then Close #1
    'err_exit:
        MsgBox Err.Description
        'MsgBox "要导入的文件未找到,请确认硬盘有此TXT文件"
    End Sub
      

  6.   

    谢谢:: qingming81(晴明) 
    新年快乐.
      

  7.   

    to qingming81(晴明) :程序运行后不对啊,
    结果第一行没有改动,从第二行开始显示的形式还是:
    03.12.23|00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
      

  8.   

    再改如下(吃饭的时候想起来就知道有问题)
    Private Sub FormatTxt(strFromName As String, strToName As String)
    '开始格式化文本文件
        Dim i as long
        dim j as long
        Dim strTmp As String
        Dim strLine() as string
        Dim strArray() As String    Open strFromName For Input As #1
        strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
        Close #1
        
        strArray = Split(strTmp, vbCrLf)
        strTmp =""
        For i = 0 To UBound(strArray)
           strLine=split(strarray(i),vbtab)
           for j=0 to Ubound(strLIne)
              if j=0 then
                  strTmp =strTmp & strLine(j)
              elseif j=1 then
                  strTmp =strTmp & " " & strline(j)
              else
                  strTmp =strTmp & "|" & strline(j) 
              end if
           next j
           strTmp=strTmp & vbcrlf
        Next i
        
        open strtoname for output as #1
        print #1,strTmp
        Close #1
         exit sub
    End Sub
      

  9.   

    还是不对啊,这次运行后test.txt记录没有一点改变只是新建了一个zz.txt文件,里面的记录格式与test.txt内容一样的.
      

  10.   

    我想问一个问题:
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    03.12.23与00:08:11和103123460010,它们之间是空格呢还是vbtab?
    strLine=split(strarray(i),vbtab)的意思是按vbtab拆分。如果是空格
    将这句改为
    strLine=split(strarray(i)," ")  双引号中是一个空格因为我看小马哥的代码是strLine=split(strarray(i),vbtab),就认为你原文是间隔的vbtab。Private Sub FormatTxt(strFromName As String, strToName As String)
    '开始格式化文本文件
        Dim i as long
        dim j as long
        Dim strTmp As String
        Dim strLine() as string
        Dim strArray() As String    Open strFromName For Input As #1
        strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
        Close #1
        
        strArray = Split(strTmp, vbCrLf)
        strTmp =""
        For i = 0 To UBound(strArray)
           strLine=split(strarray(i)," ")      '注意,一定是空格相间才这样!
           for j=0 to Ubound(strLIne)
              if j=0 then
                  strTmp =strTmp & strLine(j)
              elseif j=1 then
                  strTmp =strTmp & " " & strline(j)
              else
                  strTmp =strTmp & "|" & strline(j) 
              end if
           next j
           strTmp=strTmp & vbcrlf
        Next i
        
        open strtoname for output as #1
        print #1,strTmp
        Close #1
    End Sub
      

  11.   

    Private Sub FormatTxt(strFromName As String, strToName As String)
    strFromName是原文件路径
    strToName是生成新文件的路径
      

  12.   

    我觉得kweis(可微思)的方法不错了,,不可以吗?
      

  13.   

    只是楼主要求第一空格不替换,故可按用split函数来处理!!
      

  14.   

    谢谢qingming81(晴明) ,我想应是可以了,
    明天来结贴.
    再次谢谢.
      

  15.   

    to qingming81(睛明):
    您好我运行你的代码了但还是不行啊,
    程序进入死循环,然后死机.
    怎么回事啊.???
      

  16.   

    其实重点就一个∶要有选择得替换空格
    1.首先我们定义一个标识符,用来记录当前得空格是否要转换dim txtflag as integer
    2.再看有什么规律∶
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    03.12.23后面得空格不替换。即是说遇到"."(ASCII(46))我们将标识符置为1:txtflag = 1
    此后,我们遇到空格(ASCII(32))就没变化。如果遇到";"(ASCII(59))我们就将标识符置为0:txtflag = 0。
    3.将规律找出来了事情就好办了。首先我们将当前文本保存在一个字符串变量中。(别告诉我你的文本NNN大。)然后通过WHILE循环读入,判断是否为空格。根据你每段的后面是否有空格。(chr(10)&chr(13)之前是否有空格)处理每小一段。
    4.既然每一小段都可以处理了。那么你就去做吧。别告诉我要帮你写代码哟。自己动手丰衣足食。哈哈
      

  17.   

    1.打开文件
    2.一行一行的读取
    3.如Replace("abc d e f"," ","|")即返回值为"abc|d|e|f"(可思微)
    4.去掉第一个"|"
    5.写到新的文件中
      

  18.   

    好朋友,我测试了根本没有问题Option ExplicitPrivate Sub FormatTxt(strFromName As String, strToName As String)
    '开始格式化文本文件
        Dim i As Long
        Dim j As Long
        Dim strTmp As String
        Dim strLine() As String
        Dim strArray() As String    Open strFromName For Input As #1
        strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
        Close #1
        
        strArray = Split(strTmp, vbCrLf)
        strTmp = ""
        For i = 0 To UBound(strArray)
           strLine = Split(strArray(i), " ")   '注意,一定是空格相间才这样!
           For j = 0 To UBound(strLine)
              If j = 0 Then
                  strTmp = strTmp & strLine(j)
              ElseIf j = 1 Then
                  strTmp = strTmp & " " & strLine(j)
              Else
                  strTmp = strTmp & "|" & strLine(j)
              End If
           Next j
           strTmp = strTmp & vbCrLf
        Next i
        
        Open strToName For Output As #1
        Print #1, strTmp
        Close #1
    End SubPrivate Sub Form_Load()
       Call FormatTxt(App.Path & "\xxx.txt", App.Path & "\zz.txt")
    End Sub那个xxx.txt里面拷贝了你上面的那个样式如下
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    03.12.23 00:08:11 103123460010 00014 1244 399 27008 27007 0 0 1 27014
    生成的文件如下
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014
    03.12.23 00:08:11|103123460010|00014|1244|399|27008|27007|0|0|1|27014不过还是在上面加上一句:主要是替换的那个循环
        For i = 0 To UBound(strArray)
           if len(strArray(i))=0 then goto handler   '不处理零字段的,进入下一个循环。
           strLine = Split(strArray(i), " ")   '注意,一定是空格相间才这样!
           For j = 0 To UBound(strLine)
              If j = 0 Then
                  strTmp = strTmp & strLine(j)
              ElseIf j = 1 Then
                  strTmp = strTmp & " " & strLine(j)
              Else
                  strTmp = strTmp & "|" & strLine(j)
              End If
           Next j
           strTmp = strTmp & vbCrLf
    handler:
        Next i
    我估计你可能是其它方面存在问题。
      

  19.   

    to qingming81(晴明) 已给你发短息了谢谢,新年快乐
      

  20.   

    to qingming81(晴明) 你好那个文本文件列的间隔没有什么规则,有些是一个空格有些是多个空格。
    要怎样处理呢,
    麻烦再帮我看看,谢谢
      

  21.   

    可不可以不写程序?用excel打开那个文本文件,它提示你选择分隔符时选1个或多个空格作为分隔符,然后再把它保存成为以“|”分隔的文本文件。
    如果一定要写代码,也可以考虑调用office,因为这毕竟是它们的强项。