问题描述
这是我要阅读的数据:
"Adam C. Emality","1Z620Y1V034826","14.40" "Ethel Baeron","1Z620Y1V034604","15.19" "Donna Lidt","1Z620Y1V034650","12.37"
然后阅读数据后,我想对两个集合进行加入,一个数组和一个列表 - 下面的我的代码.但是,执行读取文件行后,我的字符串像"\"Adam C. Emality\"" "\"1Z620Y1V034826\"" "\"14.40\"" ...等.我不想包括",我不知道为什么它在\中添加.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FileHelpers; using Parser; namespace Amazon_File { class SpreadSheet { public void create(IEnumerable<SpreadList> list) { var steamengine = new FileHelperEngine<Records>(); var records = steamengine.ReadFile(@"C:\Users\Danny\Documents\Visual Studio 2013\Projects\Amazon File\Amazon File\Daniel.csv"); var spreadlist = from x in list join y in records on x.Name equals y.Name select new { y.Name, y.Track, y.worldPrice, x.ItemPrice, x.Quantity }; [DelimitedRecord(",")] public class Records { public string Name; public string Track; public string worldPrice; } public class SpreadList { public string Name { get; set; } public string Title { get; set; } public string ItemPrice { get; set; } public string Quantity { get; set; } } }
推荐答案
[DelimitedRecord(",")] public class Records { [FieldQuoted] public string Name; [FieldQuoted] public string Track; [FieldQuoted] public string worldPrice; }
其他推荐答案
A CSV解析器现在是.NET框架的一部分.
添加Microsoft.VisualBasic.dll的引用(在C#中正常工作,不要介意名称)
using (TextFieldParser parser = new TextFieldParser(@"c:\temp\test.csv")) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); while (!parser.EndOfData) { //Process row string[] fields = parser.ReadFields(); foreach (string field in fields) { //TODO: Process field } } parser.Close(); }
文档是在这里 > >
其他推荐答案
这是因为您将文件读为扁平字符串.该文件包含引号,因此它们也被阅读.这是标准的CSV格式.
斜线在C#中用作逃生字符.所以\"真的只是".您需要用空,修剪引号,或使用CSV库正确读取文件中的报价.就个人而言,我以前使用过CSVHelper,而且很棒.您可以从Nuget Package Manager获得它.
例如.
var newstring = oldstring.Replace("\"", string.Empty);
您是否可以访问FileHelperengine?如果是这样,您应该查看其解析文件的方式.否则,您可以在列表上迭代并将报价剥离或将其剥离在对象的设置中或您认为适合解决方案的任何内容中.
问题描述
Here's the data I want to read:
"Adam C. Emality","1Z620Y1V034826","14.40" "Ethel Baeron","1Z620Y1V034604","15.19" "Donna Lidt","1Z620Y1V034650","12.37"
Then after reading the data in, I want to perform a Join on the two collections, one an array and one a list - my code below. However after executing the read file line my strings are stored like this "\"Adam C. Emality\"" "\"1Z620Y1V034826\"" "\"14.40\""...etc.. Why is this happening? I do not want to include the " and I don't know why it is adding in a \.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FileHelpers; using Parser; namespace Amazon_File { class SpreadSheet { public void create(IEnumerable<SpreadList> list) { var steamengine = new FileHelperEngine<Records>(); var records = steamengine.ReadFile(@"C:\Users\Danny\Documents\Visual Studio 2013\Projects\Amazon File\Amazon File\Daniel.csv"); var spreadlist = from x in list join y in records on x.Name equals y.Name select new { y.Name, y.Track, y.worldPrice, x.ItemPrice, x.Quantity }; [DelimitedRecord(",")] public class Records { public string Name; public string Track; public string worldPrice; } public class SpreadList { public string Name { get; set; } public string Title { get; set; } public string ItemPrice { get; set; } public string Quantity { get; set; } } }
推荐答案
You must add [FieldQuoted] to make the library auto remove them http://www.filehelpers.net/docs/html/T_FileHelpers_FieldQuotedAttribute.htm
[DelimitedRecord(",")] public class Records { [FieldQuoted] public string Name; [FieldQuoted] public string Track; [FieldQuoted] public string worldPrice; }
其他推荐答案
A CSV parser is now a part of .NET Framework.
Add a reference to Microsoft.VisualBasic.dll (works fine in C#, don't mind the name)
using (TextFieldParser parser = new TextFieldParser(@"c:\temp\test.csv")) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); while (!parser.EndOfData) { //Process row string[] fields = parser.ReadFields(); foreach (string field in fields) { //TODO: Process field } } parser.Close(); }
The docs are Here
其他推荐答案
This is happening because you are reading the file as a flat string. The file contains quotes and therefore they are also getting read in. This is standard CSV format.
The slash is used as an escape character in C#. So \" is really just ". You need to either replace the quotes with empty, trim the quotes, or use a CSV library to properly read in the file. Personally, I've used CSVHelper before and it has been great. You can get it from nuget package manager.
e.g.
var newstring = oldstring.Replace("\"", string.Empty);
Do you have access to FileHelperEngine? If so, you should look at how it parses the file. Otherwise, you can iterate over the list and strip the quotes out or strip them out in the object's setters or whatever you think is appropriate for your solution.