Results 1 to 5 of 5

Thread: Does anyone have any comma sense out there?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    Cool Does anyone have any comma sense out there?

    Greetings,

    Attached is a zip file containing a csv file. I am writing a program that needs to parse the file for importing its items to a database.

    In toggling through the code I am using, for line input, instead of givng the whole line it sometimes gives the last word or number of a line, preceeded by a bunch of commas.

    Here is what I am asking:
    In parsing a .csv file, in the context of the "while not eof() loop", how can I input each "line", soas to make sure I see all the information and can parse it?

    Right now, the "tmp" values are missing some stuff that, I think should be appearing? In other words, I am not capturing all the data on a line with the Line Input command.

    Do While Not EOF(1)
    Line Input #1, tmp
    Loop

    Can someone give me some direction on this (i.e. the best way to parse a .csv file, like the one attached)?

    Thank you,
    Jim
    Attached Files Attached Files

  2. #2

  3. #3
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Generally, I like useing Binary, ie.. Something like:
    {assuming your line seperators ar vbCrLf}

    VB Code:
    1. Private Type MY_DAT
    2.         field1 As String
    3.         field2 As Integer
    4.         field3 As Long
    5.         filed4 As String
    6.     End Type
    7.  
    8.     Dim MyRecs() As MY_DAT
    9.  
    10. Private Sub Command1_Click()
    11.     Dim MyF As Integer
    12.     Dim MyI As Long
    13.     Dim MyJ As Long
    14.     Dim MatchesSoFar As Long
    15.    
    16.     Dim MyStr As String
    17.     Dim MyStuff As Variant
    18.     Dim MyStuff2 As Variant
    19.    
    20.     Dim MyNumberOfFields As Integer
    21.         MyNumberOfFields = 3 '   1 less than the total, since split returns a 0 based array
    22.         MyF = FreeFile
    23.         Open yourfile For Binary As #MyF
    24.             MyStr = Space(LOF(MyF))
    25.             Get #MyF, , MyStr
    26.         Close #MyF
    27.         MyStuff = Split(MyStr, vbCrLf, -1, vbTextCompare)
    28.         ReDim MyRecs(UBound(MyStuff))
    29.         For MyI = 0 To UBound(MyStuff)
    30.             MyStuff(MyI) = Trim(MyStuff(MyI))
    31.             If MyStuff(MyI) <> "" Then
    32.                 If InStr(1, MyStuff(MyI), ",", vbTextCompare) <> 0 Then
    33.                     MyStuff2 = Split(MyStuff(MyI), ",", -1, vbTextCompare)
    34.                     If UBound(MyStuff2) = MyNumberOfFields Then
    35.                         MyRecs(MatchesSoFar).field1 = MyStuff2(0)
    36.                         MyRecs(MatchesSoFar).field2 = CInt(MyStuff2(1))
    37.                         MyRecs(MatchesSoFar).field3 = CLng(MyStuff2(2))
    38.                         MyRecs(MatchesSoFar).field4 = MyStuff2(3)
    39.                         MatchesSoFar = MatchesSoFar + 1
    40.                     End If
    41.                 End If
    42.             End If
    43.         Next MyI
    44.         ReDim Preserve MyRecs(MatchesSoFar - 1)
    45.  
    46. End Sub

    -Lou

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    dim tmp() as string, big as string
    dim row() as string
    open "myfile.csv" for input as #1
    big=input(lof(1),1)
    close #1
    tmp=Split(big,VbCrLf)
    for i = 1 to ubound(tmp)
           row=Split(tmp(i),",")
           for j=1 to ubound(row)
                 .............. each row(j) is a cell
          next j
          Erase row
    next i

  5. #5
    Fanatic Member joltremari's Avatar
    Join Date
    Sep 2000
    Location
    Mississippi
    Posts
    674
    I'm not exactly sure if I'm clear on what your saying, but, if you've been viewing this file in excel you should try viewing it in notepad then you'll see where those commas followed by one entry at the end. Lines 3, 4, & 5 are like that. Because this is saved as a .csv file it defaults to excel, so therefore if you type one value in the last column all the columns before it will still get seperated by commas even though they have no values. (lines 3,4,&5 in your file, also lines 1 & 2.)

    Also instead of getting each line by a while not eof loop you should maybe try loading each line into an array, like so:
    VB Code:
    1. Dim MyLine() As String
    2.  
    3. Open [I]filename[/I] for Input As #1
    4.     MyString = Split(Input(LOF(1), 1), vbCrLf)
    5. Close #1
    6.  
    7. 'Then you can access each line like so:
    8.  
    9. MyString(0) 'Line 1
    10. MtString(1) 'Line 2  and so on....
    11.  
    12. 'Or you could loop through them...
    13.  
    14. For i = 0 to Ubound(MyLine)
    15.         'Do something with the line here
    16.         'Like with another array
    17.     strTemp = MyLine(i)
    18.     MyNewLine = Split(strTemp, ",")
    19.         'Or what ever you need to do here
    20. Next i
    "I have not failed. I've just found 10,000 ways that won't work."
    'Thomas Edison'

    "If we knew what it was we were doing it wouldn't be called research, would it?"
    'Albert Einstein'

    VB6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width