问题
I want to import text data from Google Finance, and I use this http address as a parameter to DownloadString
http://www.google.com/finance/getprices?i=1200&p=1d&f=d,o,h,l,c,v&df=cpct&q=AAPL
. However, the resulting string misses any newline characters, so it is really difficult to parse. Any ideas?
回答1:
The line ends returned from the stream are \n
opposed to the default Windows line ends \r\n
(which is represented in Environment.NewLine
on Windows).
Try to split on all of the possible combinations of \r
and \n
:
WebClient wc = new WebClient();
string s = wc.DownloadString("http://www.google.com/finance/getprices?i=1200&p=1d&f=d,o,h,l,c,v&df=cpct&q=AAPL");
string[] lines = s.Split(new string[] { Environment.NewLine, "\n", "\"r" }, StringSplitOptions.None);
回答2:
There ARE newline characters in the file. Check it in a hex editor. They are Unix line-endings, \n (0x0A), not Windows line-endings, \r\n (0x0D 0x0A). You can feed your string to a StringReader
and then read it line by line, and then write it line by line to somewhere else, to normalize line endings, or you can just do a replace operation.
DownloadString
does NOT alter the downloaded contents, the only problem could be mismatched encodings.
来源:https://stackoverflow.com/questions/26928158/downloadstring-skips-newline-characters