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)?
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
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:
Dim MyLine() As String
Open [I]filename[/I] for Input As #1
MyString = Split(Input(LOF(1), 1), vbCrLf)
Close #1
'Then you can access each line like so:
MyString(0) 'Line 1
MtString(1) 'Line 2 and so on....
'Or you could loop through them...
For i = 0 to Ubound(MyLine)
'Do something with the line here
'Like with another array
strTemp = MyLine(i)
MyNewLine = Split(strTemp, ",")
'Or what ever you need to do here
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'