我有一个包含逗号分隔文本的字符串。逗号分隔的文本来自一个 excel .csv 文件,因此有数百行七列宽的数据。此文件中的一行示例如下:
2012-10-01,759.05,765,756.21,761.78,3168000,761.78
我想按第一列中的日期搜索数百行。一旦我找到正确的行,我想提取逗号分隔字符串的第一个位置的数字,所以在这种情况下,我想提取数字 759.05 并将其分配给变量“Open”。
到目前为止,我的代码是:
strURL = "http://ichart.yahoo.com/table.csv?s=" & tickerValue
strBuffer = RequestWebData(strURL)
Dim Year As String = 2012
Dim Quarter As String = Q4
If Quarter = "Q4" Then
Dim Open As Integer =
End If
一旦我可以将其缩小到正确的行,我认为类似 row.Split(",")(1).Trim) 的东西可能会起作用。
我已经做了很多研究,但我自己无法解决这个问题。有什么建议么!?!
附加信息:
Private Function RequestWebData(ByVal pstrURL As String) As String
Dim objWReq As WebRequest
Dim objWResp As WebResponse
Dim strBuffer As String
'Contact the website
objWReq = HttpWebRequest.Create(pstrURL)
objWResp = objWReq.GetResponse()
'Read the answer from the Web site and store it into a stream
Dim objSR As StreamReader
objSR = New StreamReader(objWResp.GetResponseStream)
strBuffer = objSR.ReadToEnd
objSR.Close()
objWResp.Close()
Return strBuffer
End Function
更多附加信息:
我的代码的更完整的图片
Dim tickerArray() As String = {"GOOG", "V", "AAPL", "BBBY", "AMZN"}
For Each tickerValue In Form1.tickerArray
Dim strURL As String
Dim strBuffer As String
'Creates the request URL for Yahoo
strURL = "http://ichart.yahoo.com/table.csv?s=" & tickerValue
strBuffer = RequestWebData(strURL)
'Create Array
Dim lines As Array = strBuffer.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
'Add Rows to DataTable
dr = dt.NewRow()
dr("Ticker") = tickerValue
For Each columnQuarter As DataColumn In dt.Columns
Dim s As String = columnQuarter.ColumnName
If s.Contains("-") Then
Dim words As String() = s.Split("-")
Dim Year As String = words(0)
Dim Quarter As String = words(1)
Dim MyValue As String
Dim Open As Integer
If Quarter = "Q1" Then MyValue = Year & "-01-01"
If Quarter = "Q2" Then MyValue = Year & "-04-01"
If Quarter = "Q3" Then MyValue = Year & "-07-01"
If Quarter = "Q4" Then MyValue = Year & "-10-01"
For Each line In lines
Debug.WriteLine(line)
If line.Split(",")(0).Trim = MyValue Then Open = line.Split(",")(1).Trim
dr(columnQuarter) = Open
Next
End If
Next
dt.Rows.Add(dr)
Next
现在在For Each line in lines
循环中,Debug.WriteLine(line)
输出 2,131 行:
从
Date,Open,High,Low,Close,Volume,Adj Close
2013-02-05,761.13,771.11,759.47,765.74,1870700,765.74
2013-02-04,767.69,770.47,758.27,759.02,3040500,759.02
2013-02-01,758.20,776.60,758.10,775.60,3746100,775.60
一路...
2004-08-19,100.00,104.06,95.96,100.34,22351900,100.34
但是,我期望的是在循环Debug.WriteLine(line)
中一次输出一行。For Each line in lines
所以我希望第一个输出是Date,Open,High,Low,Close,Volume,Adj Close
,下一个输出是2013-02-05,761.13,771.11,759.47,765.74,1870700,765.74
. 我预计这会发生 2,131 次,直到最后一个输出是2004-08-19,100.00,104.06,95.96,100.34,22351900,100.34