简体   繁体   中英

FileHelpers: How to handle quoted fields when reading file

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.

eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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