parsing a csv file

rjhe22

Well-known member
Joined
May 22, 2008
Messages
88
Programming Experience
Beginner
hi
we are trying to use google to get exchange rate prices.
they use a csv file which we get the code to go out and get.
what im trying to do is pick the first line of the file and just use that. i have the following code but its not just picking the one line.
anyone any ideas

VB.NET:
Dim strURL As String
        'Creates the request URL for Yahoo
        strURL = "http://www.google.com/finance/historical?q="
        strURL &= Me._symbol
       strURL &= "&output=csv"

        Dim req As System.Net.WebRequest
        Dim resp As System.Net.WebResponse

        req = System.Net.HttpWebRequest.Create(strURL)
        Try
            resp = req.GetResponse()

            'Read the answer from the Web site and store it into a stream
            Me._reader = New IO.StreamReader(resp.GetResponseStream)

           

            Me._reader.ReadLine()
            ' reads the second line and splits it into a string array.   
            Dim firstLineOfData As String() = Me._reader.ReadLine().Split(New Char() {","c})
            MsgBox(firstLineOfData)


            ' first field in array   
            Dim theDate As Date = Date.Parse(firstLineOfData(0))

            'Parse the header to create columns
            Me._history = New DataTable("YahooLastPrice")

            'Parse the body of the downloaded data
            Me.header(Me._history)

            Me.body(Me._history)
            resp.Close()
 
Hi rjhe22.
This is my first post :)

Everytime you call "Me._reader.ReadLine()", it will read one line of text from your csv. Once this is done, the reader moves to the next line, ready to read.
I see that you call "Me._reader.ReadLine()" twice in your code.
The first time you call it, the reader reads the first line, but does not store the line into a variable or do anything else with it, so it gets discarded.
The second time you call it, the reader reads the second line, and then you store that second line into a String Array called "firstLineOfData"

If you remove the first occurence of "Me._reader.ReadLine()" from your code, the reader will read the first line from the csv and store THAT into the array called "firstLineOfData"
 
hi
thanks for reply will try that and see will it work.
if it does i will let you know and will very very greatful
 
get this error
The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
at this line
VB.NET:
            Dim theDate As Date = Date.Parse(firstLineOfData(0))
 
Can you show me the content of the CSV file so that I can get a better idea what you are working with?
 
try this

This gives me the correct output.
Check if it works for you.

VB.NET:
Dim strURL As String
        'Creates the request URL for Yahoo

        strURL = "http://www.google.com/finance/historical?q=NYSE:C&output=csv"

        Dim req As System.Net.WebRequest
        Dim resp As System.Net.WebResponse

        req = System.Net.HttpWebRequest.Create(strURL)
        resp = req.GetResponse()

        'Read the answer from the Web site and store it into a stream
        Dim _reader As IO.StreamReader
        _reader = New IO.StreamReader(resp.GetResponseStream)


        ' skip the first line of column headers
        _reader.ReadLine()

        ' reads the second line and splits it into a string array.   
        Dim firstLineOfData As String() = _reader.ReadLine().Split(New Char() {","c})

        ' Display the first row content, just for testing purposes
        Dim testString As String = ""
        testString &= "Date: " & firstLineOfData(0) & vbCrLf
        testString &= "Open: " & firstLineOfData(1) & vbCrLf
        testString &= "High: " & firstLineOfData(2) & vbCrLf
        testString &= "Low: " & firstLineOfData(3) & vbCrLf
        testString &= "Close: " & firstLineOfData(4) & vbCrLf
        testString &= "Volume: " & firstLineOfData(5) & vbCrLf
        MsgBox(testString)

        resp.Close()
 
Back
Top