简体   繁体   English

DownloadString跳过换行符

[英]DownloadString skips newline characters

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 . 我想从Google财经导入文本数据,并使用此http地址作为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? 有任何想法吗?

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). 它们是Unix行结尾,\\ n(0x0A),而不是Windows行结尾,\\ 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. 您可以将字符串提供给StringReader ,然后逐行读取,然后逐行将其写入其他位置,以标准化行结尾,或者您可以只执行替换操作。

DownloadString does NOT alter the downloaded contents, the only problem could be mismatched encodings. DownloadString不会改变下载的内容,唯一的问题可能是编码不匹配。

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). 从流返回的行结尾是\\n而不是默认的Windows行结束\\r\\n (在Windows上的Environment.NewLine表示)。

Try to split on all of the possible combinations of \\r and \\n : 尝试拆分\\r\\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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM