【发布时间】:2015-11-29 15:52:34
【问题描述】:
我试图将字符串拆分为子部分以传递到 linq 数据库,但我遇到了一个问题。该文件是一个 .csv 文件,因此用逗号分隔:
1,Ms,Aleshia,Tomkiewicz,14 Taylor St,St.斯蒂芬斯病房,肯特,CT2 7PP,01835-703597,atomkiewicz@hotmail.com.
但是,某些数据在数据字段中包含逗号,例如县/地址用逗号分隔,但我不希望它拆分我希望它将这些数据保持在一起,例如地址:伦敦,温布尔登。
im using this code currently to do the chopping:
public static List<string> ReturnCSVFromWeb(string url)
{
System.Net.WebClient client = new WebClient();
string CSVContent = client.DownloadString(url);
List<string> splitted = new List<string>();
string csvFile = CSVContent;
string[] tempStr;
tempStr = csvFile.Split(',','\n');
foreach (string item in tempStr)
{
if (!string.IsNullOrWhiteSpace(item))
{
splitted.Add(item);
}
}
return splitted;
}
【问题讨论】:
-
请Stop Rolling Your Own CSV Parser。还要尊重数据的标准格式。如果不应该分隔
London,Wimbledon,您似乎没有 CSV,而是一些自定义格式。您应该首先考虑如何正确编码您的数据。 -
不允许使用仅标准的 .Net 库的 3rd 方软件
-
那么最好做好下到深渊的准备。请不要用
CSV标记您的问题,因为显然这不是您输入的格式。而是指定您有一些某人发明的自定义格式,并确保您在尝试解析之前非常精确地定义了这种格式。 -
您确定地址值不只是单个列吗?
-
对不起,我的意思是我们在一个字段中有这样的地址:Broxburn、Uphall 和 Winchburg,用逗号分隔