FileHelpers: How to handle quoted fields when reading file

你离开我真会死。 提交于 2019-12-11 03:30:57

问题


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; }
    }
}

回答1:


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;
}   



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/32105732/filehelpers-how-to-handle-quoted-fields-when-reading-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!