字符串问题中额外的行和逗号

本文关键字:问题 字符串 | 更新日期: 2023-09-27 18:16:05

我有下面的代码,输入一个CSV文件,然后转换它并输出一个CSV文件。我正在使用FileHelper进行CSV读取。我对它有一些问题。首先,插入带有字段信息的额外行(见下文)。第二个问题是,我添加了双引号,所以我可以在单元格中有一个包含逗号的字符串;我找不到任何错误的格式,但它不会导入excel,只显示双引号和第一项。我曾尝试使用双引号转义文本选项时导入excel。第三个问题是有些字段是空白的,导致后面的字段偏移。

第二行字段:

, base, 0, 0, 0, Product Name, Size, Brand, Gender, Type, SKU, Stock, Sí, "Photo1, Photo2, Photo3", Photo1, Photo1, Photo1
代码:

using System;
using FileHelpers;
using System.IO;
[DelimitedRecord(";")]
public sealed class StoreItems
{
[FieldOptional()]
public String Attribut;
[FieldOptional()]
public String ProductName;
[FieldOptional()]
public String Size;
[FieldOptional()]
public String Brand;
[FieldOptional()]
public String Type;
[FieldOptional()]
public String ParentSKU;
[FieldOptional()]
public String SKU;
[FieldOptional()]
public String Stock;
[FieldOptional()]
public String RetailEURO;
[FieldOptional()]
public String SuggestedsellEURO;
[FieldOptional()]
public String Weight;
[FieldOptional()]
public String Description;
[FieldOptional()]
public String Photo1;
[FieldOptional()]
public String Photo2;
[FieldOptional()]
public String Photo3;
[FieldOptional()]
public String Gender;
[FieldOptional()]
public String EAN13;
}
public class RunProgram
{
    public static void Main()
    {
        FileHelperEngine engine = new FileHelperEngine(typeof(StoreItems));
        StoreItems[] res = engine.ReadFile("Presta.csv") as StoreItems[];
        Directory.CreateDirectory("Output");
        StreamWriter sw = new StreamWriter(@".'Output'" + DateTime.Now.ToString("hhmmss") + ".csv");
        sw.AutoFlush = true;
        Console.SetOut(sw);
        Console.WriteLine("_type, _product_websites, tax_class_id, _visibility, is_in_stock, name, taglia, manufacturer, gender, _attribute_set, sku, qty, use_external_images, external_gallery, " +
            "thumbnail_external_url, small_image_external_url, image_external_url");
        foreach(StoreItems item in res)
        {
            string newtype = null;
            int visibility = 0;
            if (item.Attribut == "Parent")
            {
                newtype = "Configurable";
                visibility = 4;
            }
            else if (item.Attribut == "Child")
            {
                newtype = "Simple";
                visibility = 1;
            }
            int isinstock;
            int stockint;
            try
            {
               stockint = Convert.ToInt32(item.Stock);
            }
            catch
            {
               stockint = 0;
            }
            if (stockint > 0)
            {
                isinstock = 1;
            }
            else
            {
                isinstock = 0;
            }

            Console.WriteLine(newtype + ", base, 0, " + visibility + ", " + isinstock + ", " + item.ProductName + ", " + item.Size + ", " +
                item.Brand + ", " + item.Gender + ", " + item.Type + ", " + item.SKU + ", " + item.Stock + ", Sí, " + @""""+item.Photo1+ ", "+item.Photo2+", "+item.Photo3+@"""" + ", " + item.Photo1 + ", " + item.Photo1 +
                ", " + item.Photo1);
        }

    }

}

编辑:我刚刚意识到第二行是从输入文件的读取中添加的。我只需要想办法忽略第一行。这些领域似乎也不再抵消。现在只是如何在csv中使用逗号?

字符串问题中额外的行和逗号

第二个问题;

你不必使用逗号作为分隔符,你可以使用任何你想要的字符。因此,作为一个快速解决方案,为什么不将输出中的','分隔符更改为'|'呢?

如果,当然,你也需要在你的文本中输出'|',这是不会有帮助的,但如果不是-快速修复让你继续

一个问题;你有;

string newtype = null;
int visibility = 0;
if (item.Attribut == "Parent")
{
    newtype = "Configurable";
    visibility = 4;
}
else if (item.Attribut == "Child")
{
    newtype = "Simple";
    visibility = 1;
}

如果item.Attribut既不是"Parent"也不是"Child",则不设置newtype。这就是为什么示例输出中的第一个字段为空。