Extracting Information from a CSV File | Newbie | | Join Date: Jan 2007
Posts: 9
| | |
Hi,
I've got the following in a txt file (it could just as easily be a csv file):
"Conroy","West","1618 TAMARACK ROAD","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","6240","321","ENFRB","Y"
"Larry","Todd","1419 Cleveland Blvd","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","6240","321","ENFRB","Y"
Each record is on it's own line. How would I extract the information from each field for each record?
Thanks,
Keith
|  | Expert | | Join Date: Jul 2006
Posts: 250
| | | re: Extracting Information from a CSV File Quote:
Originally Posted by kjflash Hi,
I've got the following in a txt file (it could just as easily be a csv file):
"Conroy","West","1618 TAMARACK ROAD","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","6240","321","ENFRB","Y"
"Larry","Todd","1419 Cleveland Blvd","","OWENSBORO","KY","42301-1234","11/05/2005","2706824454","6240","321","ENFRB","Y"
Each record is on it's own line. How would I extract the information from each field for each record?
Thanks,
Keith Is this VB6/VBA/VBScript/VB.Net?
CSV is a PITA to parse IMO, I usually do tab-delimited with no text qualifier when program allows it and I know there are not any tabs in the fields.
Some programs export CSV differently than others. This will only work with the sample data you provided (VB6/VBA/VBScript). - Private Sub Command1_Click()
-
Dim sMyFilePath As String, sMyLineofText As String
-
Dim lngFile As Long
-
-
sMyFilePath = "c:\test.txt"
-
-
'set lngFile variable to an value from other open files
-
lngFile = FreeFile
-
-
'open file and for input
-
Open sMyFilePath For Input As #lngFile
-
-
'read each line of the file
-
Do While Not EOF(lngFile)
-
-
'set sMyLineofText variable to the line of text VB reads from file
-
Line Input #lngFile, sMyLineofText
-
-
'do your commands based on the text in sMyLineofText
-
-
'Remove quote at beginning and end
-
If Len(sMyLineofText) > 1 Then sMyLineofText = Mid(sMyLineofText, 2, Len(sMyLineofText) - 2)
-
-
'split fields by ","
-
artmp = Split(sMyLineofText, """,""")
-
-
'Check to see that line was parsed correctly, should have an exact number of expected elements
-
'the array is 0 based so if you have 13 fields check for a ubound of 12
-
If UBound(artmp) <> 12 Then
-
MsgBox "Line not parsed correctly"
-
Exit Sub
-
End If
-
-
'Loop through the fields (for example)
-
For i = 0 To UBound(artmp)
-
Debug.Print "Field" & i & ": " & artmp(i)
-
Next 'i
-
-
'Or specify a field manually
-
Debug.Print artmp(0) '1st field (first name)
-
Debug.Print artmp(1) '2nd field (last name)
-
Debug.Print artmp(2) '3rd field (address)
-
Debug.Print artmp(3) '4th field (address2)
-
Debug.Print artmp(4) '5th field (city)
-
Debug.Print artmp(5) '6th field (State)
-
Debug.Print artmp(6) '7th field (zip)
-
Debug.Print artmp(7) '8th field (a date)
-
Debug.Print artmp(8) '9th field (??)
-
Debug.Print artmp(9) '10th field (??)
-
Debug.Print artmp(10) '11th field (??)
-
Debug.Print artmp(11) '12th field (??)
-
Debug.Print artmp(12) '13th field (??)
-
Loop
-
Close #lngFile
-
End Sub
|  | Similar Visual Basic 4 / 5 / 6 bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|