473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parsing csv file in VB (example included)

I needed to parse a csv file for my project. Could not find any so I
put together this code. It works for general cases. Use it and let me
know. Just
Cut and paste the 2 functions in a new Form and the csv text in
test.csv in the same directory. Watch for the line breaks!

'------ Parse CSV + Demo ----------------------
' (c) Shahid Mahmood (ma*****@alum.rpi.edu)
' ---------------------------------------------
' Parses a csv file and prints results in a debug window
("immediate").
' If you know how many fields per line are coming, you can diemnsion
the
' 'strTemp() array accordingly and lost the ReDim ..()
' Handles the case of quoted and un-quoted fields, (only double quoted
"")
' Also handles commas inside a quoted field. DOES not handle embedded
double
' quotes or single quoted fields.
' Enjoy!

Private Sub Form_Load()
Dim fieldArray() As String
Dim i As Integer
Dim lineRead As String
'MsgBox App.Path ' to see where VB is reading from
Open App.Path + "\" + "test.csv" For Input As #1

While Not EOF(1)
Line Input #1, lineRead
fieldArray = parseCsv(lineRead)
' Do whatever you want with the array of fields, e.g.
If lineRead <> "" Then For i = 0 To UBound(fieldArray):_
Debug.Print fieldArray(i): Next
Wend
End
End Sub

Function parseCsv(lineIn As String) As String()
Dim s As String
Dim local_s As String
Dim n1 As Integer
Dim n2 As Integer
Dim str2 As String
Dim i As Integer
Dim strTemp() As String
i = 0
s = Trim(lineIn) ' remove spaces, if any

While Len(s)
ReDim Preserve strTemp(i) ' Didnt want to have fixed fields,
' dont know how to dynamically allocate, hence ...
If Mid$(s, 1, 1) = """" Then ' if already in quotes ...
strTemp(i) = Mid$(s, 2, InStr(2, s, """") - 2)'find 2nd ""
Else
If InStr(s, ",") Then strTemp(i) = Mid$(s, 1, InStr(s, ",") -
1)
End If
's = Mid$(s, Len(strTemp(i)) + 1)
n2 = InStr(Len(strTemp(i)) Or 1, s, ",")
If n2 Then s = Trim(Mid$(s, n2 + 1)) Else s = "" ' clip till next
i = i + 1
Wend
parseCsv = strTemp
End Function
'---- END -------

Sample test.csv
---------------
"Hello, World", wow, " asdasd asdasd"
"Hello1, World1", wow1, " asdasd1 asdasd1"
"Hello2, World2", wow2, " asdasd2 asdasd2"
"This is Field1", "Field2",Field3,"Field4",field5,field6, "Field7,8,9"
Jul 17 '05 #1
0 24076

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Viktor Rosenfeld | last post by:
Hi, I need to create a parser for a Python project, and I'd like to use process kinda like lex/yacc. I've looked at various parsing packages online, but didn't find anything useful for me: -...
4
by: ArmedCoder | last post by:
Ok, i have this problem that is driving me nuts! I have a header file that has the function definition: landindp get_world_landind( worldp world ); where landindp and worldp are pointers to...
6
by: Tuomas Rannikko | last post by:
Hello, I'm currently writing a XML processor for the fun of it. There is something I don't understand in the spec though. I'm obviously missing something important. The spec states that both...
4
by: Rick Walsh | last post by:
I have an HTML table in the following format: <table> <tr><td>Header 1</td><td>Header 2</td></tr> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td></tr>...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
18
by: Steven Borrelli | last post by:
Hello, I am using the <?php include() ?statement on my website for organizational purposes. However, one of my includes contains some PHP code. Is there any way for the server to actually...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
2
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.