Extra Line and Comma in String Issues
我有以下代码输入 CSV 文件,然后将其转换并输出 CSV 文件。我正在使用 FileHelper 读取 CSV。我有几个问题。首先,正在插入带有字段信息的额外行(见下文)。第二个问题是我添加了双引号,因此我可以在包含逗号的单元格中包含一个字符串;我找不到格式有什么问题,但它不会导入到 excel 中,只显示双引号和第一项。我尝试在导入 excel 时使用双引号转义文本选项。第三个问题是有些字段是空白的,导致字段后期偏移。
第二行字段:
1 | , base, 0, 0, 0, Product Name, Size, Brand, Gender, Type, SKU, Stock, Sí,"Photo1, Photo2, Photo3", Photo1, Photo1, Photo1 |
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 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 中使用逗号?
第二个问题;
你不必使用逗号作为分隔符,你可以使用任何你想要的字符。因此,作为一个快速修复,为什么不将输出中的","分隔符更改为"|"。
当然,如果你还需要输出'|'在您的文本中,这不会有帮助,但如果没有 - 快速修复让您继续前进。
一个问题;你有;
1 2 3 4 5 6 7 8 9 10 11 12 13 | string newtype = null; int visibility = 0; if (item.Attribut =="Parent") { newtype ="Configurable"; visibility = 4; } else if (item.Attribut =="Child") { newtype ="Simple"; visibility = 1; } |
如果