简体   繁体   English

奇怪的数组问题

[英]Weird Array issue

So I am writing a C# console application.所以我正在编写一个 C# 控制台应用程序。 I have a text file that I want to send to a database.我有一个要发送到数据库的文本文件。 The plan is to have multiple text files and only one insert.计划是有多个文本文件,只有一个插入。 Everything seems to go fine for the first line. go 对于第一行来说一切似乎都很好。 Once I get to the 2nd line the array thinks it only has a length of 2.一旦我到达第二行,数组认为它的长度只有 2。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UKImporter
{
class Program
{
    static void Main(string[] args)
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\out\output.txt");
        System.Console.WriteLine("Contents of writeLines2.txt =:");

        foreach (string line in lines)
        {
            string sellername, sku, date1, quantity1, date2, asin, date3, date4, FNSKU;
            char[] tabs = { '\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t',  };
            string[] words = line.Split(tabs);
            sellername = words[0];
            sku = words[1];
            date1 = words[2];
            quantity1 = words[3];
            date2 = words[4];
            asin = words[5];
            date3 = words[6];
            date4 = words[7];
            FNSKU = words[8];
            Console.WriteLine("\t" + line);
            UKDataBLL u = new UKDataBLL();
            //u.AddToDatabase(sku, DateTime.Now, Convert.ToInt16(quantity1), DateTime.Now, asin, DateTime.Now, DateTime.Now, FNSKU);

            foreach (string s in words)
            {
                System.Console.WriteLine(s);
            }

        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

    }
}
}

Edit Here is some text of the file编辑这是文件的一些文本

A2LQ9QFN82X636 ACD_fivecrowns 6/1/11 5:30 0 6/1/11 5:30 B00000IV35 6/1/11 5:30 6/1/11 5:30 X0000PGTT9

A2LQ9QFN82X63 ACD_caylus_magna_carta 6/1/11 5:30 0 6/1/11 5:30 B000N3SOUM 6/1/11 5:30 6/1/11 5:30 X0000PGM23

A2LQ9QFN82X63 AMX_JrSpaceHelmet-LBL 6/1/11 5:30 0 6/1/11 5:30 B0008F6WMM 6/1/11 5:30 6/1/11 5:30 X0000PQBUL

You only need你只需要

 string[] words = line.Split('\t');

And then you have to verify that the contents and your operation match, a crude idea:然后你必须验证内容和你的操作是否匹配,一个粗略的想法:

 System.Diagnostics.Trace.Assert (words.Count == 9);

Using the above code, I created a simple output file step that met your intended structure and your code does correctly parse the file out.使用上面的代码,我创建了一个简单的 output 文件步骤,它符合您的预期结构,并且您的代码确实正确地解析了文件。 Your issue appears to be based on the data您的问题似乎是基于数据

    string[] content = new string[] { "a\tb\tc\td\te\tf\tg\th\ti", "a\tb\tc\td\te\tf\tg\th\ti" };
    System.IO.File.WriteAllLines(@"C:\sandbox\output.txt", content);

The tabs parameter to the split only needs to have one tab character.拆分的 tabs 参数只需要一个制表符。 You are telling it all of the possible values to split on.您正在告诉它所有可能的拆分值。 As written, you are telling it to split on a tab or a tab or and so on.如所写,您是在告诉它在选项卡或选项卡等上拆分。

It is possible that the second line is not in the correct format.第二行的格式可能不正确。

Can you send a copy of a couple of lines from the import file?您可以从导入文件中发送几行的副本吗?

I dont really see why you need to specify all those tab characters in the split method.我真的不明白为什么需要在 split 方法中指定所有这些制表符。

Just do something like this:只需执行以下操作:

foreach (string line in lines)
{
    string[] columns= line.Split('\t');
    if (columns.Length != 9) // Or how many colums the file should have.
        continue; // Line probably not valid

    // Now access all the columns of the line by using 
    // columns[0], columns[1], etc
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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