[Year 12 SofDev] Reading and Writing text data files in VB2010

BUCKNELL, Chris cbucknell at whitefriars.vic.edu.au
Thu May 12 22:16:45 EST 2011


Hi all,

To add my two cents to the conversation on processing CSV files with VB, you can use the “Microsoft.VisualBasic.FileIO” namespace and the “TextFieldParser” class.  This class provides all you need to get the data into your VB program from a CSV (or any structured file, i.e. Tabbed, fixed width, etc… ).  I’ve put a sub procedure below (written in Visual studio 2010 on .Net framework 4.0) so you can see it working.  (http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx )


    Sub readAndParseCSV(ByVal filePath As String, ByVal fileName As String)
        Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filePath & "\" & fileName)
            myReader.TextFieldType = FileIO.FieldType.Delimited
            myReader.SetDelimiters(",") 'The delimiter is a comma - as it is a CSV file
            Dim currentLine As String()

            'Loop through all of the fields in the file.
            'If any lines are corrupt, report an error and continue parsing
            While Not myReader.EndOfData
                Try
                    currentLine = myReader.ReadFields
                    'Code included to handle the data in the row.
                    Console.WriteLine()
                    For Each item As String In currentLine
                        Console.Write("~" & item & "~")
                    Next
                Catch ex As Exception
                    'Code to handle any errors in line data
                    Console.WriteLine("Line " & ex.Message & " Is not valid and will be skipped")
                End Try
            End While
            Console.ReadLine()
        End Using
    End Sub


[cid:image003.png at 01CC10F2.4793CB60]



From: sofdev-bounces at edulists.com.au [mailto:sofdev-bounces at edulists.com.au] On Behalf Of Robert Hind
Sent: Thursday, 12 May 2011 4:29 PM
To: Year 12 Software Development Teachers' Mailing List
Subject: Re: [Year 12 SofDev] Reading and Writing text data files in VB2010

Please no PSV :-)

CSV is supposed to be a "standard" - and MS Excel produces very nice CSV files using the double quotes and allows commas within the quotes. Eg

"Fred, Blogs","24 Overshot Lane, Overthetop, Vic, 3000"

So if MS Excel can produce these files, why on earth can MS VBNET not accommodate them?

Of course, following the ItApps discussion on normalisation, I would prefer to separate the individual parts of names and addresses to give us

"Fred","Blogs","24 Overshot Lane","Overthetop","Vic","3000"

Robert Hind (Semi-retired) OOF, GOM
Ashwood and Traralgon
robert at yinnar.com<mailto:robert at yinnar.com>


----- Original Message -----
From: Mark KELLY<mailto:kel at mckinnonsc.vic.edu.au>
To: Year 12 Software Development Teachers' Mailing List<mailto:sofdev at edulists.com.au>
Sent: Thursday, May 12, 2011 3:49 PM
Subject: Re: [Year 12 SofDev] Reading and Writing text data files in VB2010

Well, you could but you'd have PSV (pipe separated values) instead of CSV  :-)

Most generic textual data is CSV (with double quotes)... I don't know why VB can't accommodate them, even as an option.
On 12 May 2011 15:31, Margaret King Iaquinto <iaquinto at ozemail.com.au<mailto:iaquinto at ozemail.com.au>> wrote:
Can you use the pipe symbol (|) as a delimiter in VBNEB? In PHP that works. Then the other commas don't get in the way.

VK3CFI

On Thu May 12 12:32 , 'ATKINSON-BUCK, Damien' sent:
No, I can’t seem to find a way to do that, but as a very dodgy workaround, edit the CSV file so that there’s a double comma separating each item and split on that, then you can keep a single comma inside some text
Eg
1,,WE RUN THE NIGHT,,DJ Havana, Brown,,ISL/UMA
Dim Values() As String = Split(strAlbumName, ",")
Would split everything normally and allow a comma between Havana and Brown.

Damien Atkinson-Buck
Member of Academic Staff (Secondary)
________________________________

<http://myivanhoe.net/>

 <http://myivanhoe.net/>

PO BOX 91 The Ridgeway, Ivanhoe, Victoria 3079 Australia
Telephone +61 3 9490 3848 Facsimile +61 3 9490 3490
mailto:damien.atkinson-buck at ivanhoe.com.au
http://myivanhoe.net<http://myivanhoe.net/>


 <http://myivanhoe.net/>
 <http://myivanhoe.net/>
From: Mark KELLY [mailto:kel at mckinnonsc.vic.edu.au]
Sent: Thursday, 12 May 2011 11:19 AM
To: Year 12 Software Development Teachers' Mailing List
Subject: Re: [Year 12 SofDev] Reading and Writing text data files in VB2010<http://myivanhoe.net/>
 <http://myivanhoe.net/>
I've just tested it, and Split() does not honour quotation marks: it reads them in with the rest of the text.
So if a data item contained a comma, it would be seen as a delimiter  :-(<http://myivanhoe.net/>
On 12 May 2011 09:37, ATKINSON-BUCK, Damien <Damien.ATKINSON-BUCK at ivanhoe.com.au> wrote:<http://myivanhoe.net/>
Mark, thanks for another great tutorial. Building on Marks work might be what you need Trav<http://myivanhoe.net/>
 <http://myivanhoe.net/>
One of the more common uses of reading a text file is to read various values of a CSV (comma separated values) file, where individual items (think fields of a flat file database) are read into an array for future processing. Visual Basic has a very nice easy way to do this using the Split function. Imagine that you have the above text file with the following data copied from this week’s Aria charts;<http://myivanhoe.net/>
 <http://myivanhoe.net/>
1,WE RUN THE NIGHT,DJ Havana Brown,ISL/UMA<http://myivanhoe.net/>
2,FROM THE MUSIC,The Potbelleez,VIC/UMA<http://myivanhoe.net/>
3,WHAT HAPPENED TO USJessica Mauboy Feat. Jay Sean,SME<http://myivanhoe.net/>
4,DANCE WITH ME,Justice Crew Feat. Flo Rida,SME<http://myivanhoe.net/>
5,FRIDAY TO SUNDAY,Justice Crew,SME<http://myivanhoe.net/>
6,MAYBE,Sick Puppies,VIR/EMI<http://myivanhoe.net/>
 <http://myivanhoe.net/>
        Dim TextFile As New System.IO.StreamReader("u:\albums.txt ")<http://myivanhoe.net/>
        Dim strAlbumName As String<http://myivanhoe.net/>
        strAlbumName = TextFile.ReadLine()<http://myivanhoe.net/>
        Do Until strAlbumName Is Nothing<http://myivanhoe.net/>
            'lstAlbums.Items.Add(strAlbumName)<http://myivanhoe.net/>
            Dim Values() As String = Split(strAlbumName, ",")<http://myivanhoe.net/>
            strAlbumName = TextFile.ReadLine()<http://myivanhoe.net/>
       ' Values(0) now contains first column value,<http://myivanhoe.net/>
       ' Values(1) contains second column, etc.<http://myivanhoe.net/>
        Loop<http://myivanhoe.net/>
        TextFile.Close()<http://myivanhoe.net/>
        TextFile.Dispose()<http://myivanhoe.net/>
 <http://myivanhoe.net/>
Cheers<http://myivanhoe.net/>
Damien Atkinson-Buck
Member of Academic Staff (Secondary)<http://myivanhoe.net/>
<http://myivanhoe.net/>
________________________________


 <http://myivanhoe.net/>

PO BOX 91 The Ridgeway, Ivanhoe, Victoria 3079 Australia
Telephone +61 3 9490 3848 Facsimile +61 3 9490 3490
mailto:damien.atkinson-buck at ivanhoe.com.au
http://myivanhoe.net<http://myivanhoe.net/>


 <http://myivanhoe.net/>
 <http://myivanhoe.net/>
From: Travis Parker [mailto:Travis.Parker at beaconhills.vic.edu.au]
Sent: Thursday, 12 May 2011 9:26 AM<http://myivanhoe.net/>

To: Year 12 Software Development Teachers' Mailing List<http://myivanhoe.net/>
Subject: Re: [Year 12 SofDev] Reading and Writing text data files in VB2010<http://myivanhoe.net/>
 <http://myivanhoe.net/>
Thanks for that Mark. I have been using something similar with my students, particularly as the SAC I have written includes reading from a text file (A prototype helpdesk, including type of job, computer, priority, etc) and creating a new text file with jobs for the techies in a prioritised order.<http://myivanhoe.net/>
 <http://myivanhoe.net/>
One thing I was wondering though, does anyone have anything good on reading comma separated text files? All the programs I have gotten my students to create read them a line at a time, but I would ideally like to see something like this line - tim.cox, hardware, 12, ps40, 2 read into arrays. So I would have a “Name†, “Issue†, “Computer_Number†, “Room_Number†, “Priority†or something like that. I’m fine with the arrays, but how would I read it in word at a time (i.e. Between commas†rather than line at a time?<http://myivanhoe.net/>
 <http://myivanhoe.net/>
With many thanks for a first year SD newbie……<http://myivanhoe.net/>
 <http://myivanhoe.net/>
Trav<http://myivanhoe.net/>
 <http://myivanhoe.net/>
 <http://myivanhoe.net/>
 <http://myivanhoe.net/>
 <http://myivanhoe.net/>
 <http://myivanhoe.net/>
From: sofdev-bounces at edulists.com.au [mailto:sofdev-bounces at edulists.com.au] On Behalf Of Mark KELLY
Sent: Wednesday, 11 May 2011 12:45 PM
To: Year 12 Software Development Teachers' Mailing List
Subject: [Year 12 SofDev] Reading and Writing text data files in VB2010<http://myivanhoe.net/>
 <http://myivanhoe.net/>
Hi, you paragons of pedagogy.  I've been putting off teaching my kids how to read and write text data files in VB but having investigated today, it's not really more difficult than the days of VB6 with OPEN "file" FOR INPUT AS 1 ... INPUT #1, data.

I've attached an adapted tutorial on reading and writing text files with a task involving reading and writing a preferences file (with solution).

Once your kids can do data files, it greatly opens the scope for populating and playing with really large arrays and makes loops meaningful.

I remember how excited I was when I first discovered text files decades ago - it was like finding mum's car keys.  My programming world blossomed from that point.

--
Mark Kelly
Manager of ICT, Reporting, IT Learning Area
McKinnon Secondary College
McKinnon Rd McKinnon 3204, Victoria, Australia
Direct line / Voicemail: +613 8520 9085, Fax +613 9578 9253
kel at mckinnonsc.vic.edu.au
VCE IT Lecture Notes: http://vceit.com
Moderator: IT Applications Edulist

Want a good time? Call 0112358. Ask for Mr Fibonacci.<http://myivanhoe.net/>




Privacy, Virus and Copyright Warning

The information contained in this electronic message (e-mail), and any files transmitted with it:

* is intended for the named recipients only. If you have received this in error, please advise the sender and delete it and any copies immediately;
* Any personal information in this email must be used in accordance with the Privacy Act 1988 and this always applies even if it has been sent to you in error.
* represents the views of the sender and does not necessarily represent the views or formal advice of Ivanhoe Grammar School;
* may be subject to Copyright, so no further use should be made of it without the author's permission.

The School does not represent or warrant that the email or any files attached do not contain errors or are free from computer viruses or other defects nor does it accept responsibility for any loss or damage resulting directly or indirectly from the use of the email or any attached files. <http://myivanhoe.net/>

_______________________________________________
http://www.edulists.com.au - FAQ, Subscribe, Unsubscribe
IT Software Development Mailing List kindly supported by
http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority and
http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html
http://www.vitta.org.au  - VITTA Victorian Information Technology Teachers Association Inc<http://myivanhoe.net/>



--
Mark Kelly
Manager of ICT, Reporting, IT Learning Area
McKinnon Secondary College
McKinnon Rd McKinnon 3204, Victoria, Australia
Direct line / Voicemail: +613 8520 9085, Fax +613 9578 9253
kel at mckinnonsc.vic.edu.au
VCE IT Lecture Notes: http://vceit.com
Moderator: IT Applications Edulist

Want a good time? Call 0112358. Ask for Mr Fibonacci.<http://myivanhoe.net/>




Privacy, Virus and Copyright Warning

The information contained in this electronic message (e-mail), and any files transmitted with it:

* is intended for the named recipients only. If you have received this in error, please advise the sender and delete it and any copies immediately;
* Any personal information in this email must be used in accordance with the Privacy Act 1988 and this always applies even if it has been sent to you in error.
* represents the views of the sender and does not necessarily represent the views or formal advice of Ivanhoe Grammar School;
* may be subject to Copyright, so no further use should be made of it without the author's permission.

The School does not represent or warrant that the email or any files attached do not contain errors or are free from computer viruses or other defects nor does it accept responsibility for any loss or damage resulting directly or indirectly from the use of the email or any attached files. <http://myivanhoe.net/>


_______________________________________________
http://www.edulists.com.au - FAQ, Subscribe, Unsubscribe
IT Software Development Mailing List kindly supported by
http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority and
http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html
http://www.vitta.org.au  - VITTA Victorian Information Technology Teachers Association Inc<http://myivanhoe.net/>



--
Mark Kelly
Manager of ICT, Reporting, IT Learning Area
McKinnon Secondary College
McKinnon Rd McKinnon 3204, Victoria, Australia
Direct line / Voicemail: +613 8520 9085, Fax +613 9578 9253
kel at mckinnonsc.vic.edu.au
VCE IT Lecture Notes: http://vceit.com
Moderator: IT Applications Edulist

Want a good time? Call 0112358. Ask for Mr Fibonacci.<http://myivanhoe.net/>
<http://myivanhoe.net/>
________________________________
_______________________________________________
http://www.edulists.com.au - FAQ, Subscribe, Unsubscribe
IT Software Development Mailing List kindly supported by
http://www.vcaa.vic.edu.au - Victorian Curriculum and Assessment Authority and
http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html
http://www.vitta.org.au  - VITTA Victorian Information Technology Teachers Association Inc<http://myivanhoe.net/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20110512/1c1e4e0a/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 69086 bytes
Desc: image003.png
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20110512/1c1e4e0a/image003-0001.png 


More information about the sofdev mailing list