0

我完全不知道该怎么做,但这是我要转换的文件片段:

"September

3Beef
Lamb Chops

4Fish
Not Fish

5Mac and Cheese
PB & J"

csv 文件应该打印引号后面的日期,所以上面应该是这样的:

2013 年 9 月 3 日星期二“牛肉”“羊排”

2013 年 9 月 4 日星期三“鱼”“非鱼”

2013 年 9 月 5 日星期四“Mac and Cheese”“PB&J”

这是我到目前为止所拥有的:

StreamReader reader = new StreamReader(@"..\..\Lunches.txt");

while (!reader.EndOfStream)
{
    string currentLine = reader.ReadLine();
}

StreamWriter writer = new StreamWriter(@"..\..\Lunches.csv");

// date.ToString("ddddd yyyyy mm MMMMMM");
string delimiter = ",";
4

2 回答 2

1

这是输出

这是代码

 private void Form1_Load(object sender, EventArgs e)
    {
        string sayka = "August\n\n" +
            "31Beef\n" +
            "Lamb Chops\n" +

            "24Fish\n" +
            "Not Fish\n" +

            "15Mac and Cheese\n" +
            "PB & J\n";
        MessageBox.Show(makeCSV(sayka));
    }

    string getMonthName(int val)
    {
        switch (val)
        {
            case 1: return "JANUARY";
            case 2: return "FEBRUARY";
            case 3: return "MARCH";
            case 4: return "APRIL";
            case 5: return "MAY";
            case 6: return "JUNE";
            case 7: return "JULY";
            case 8: return "AUGUST";
            case 9: return "SEPTEMBER";
            case 10: return "OCTOBER";
            case 11: return "NOVEMBER";
            case 12: return "DECEMBER";
            default: return null;
        }
    }

    string getDayName(int val)
    {
        switch (val)
        {
            case 1: return "First";
            case 2: return "Second";
            case 3: return "Third";
            case 4: return "Fourth";
            case 5: return "Fifth";
            case 6: return "Sixth";
            case 7: return "Seventh";
            case 8: return "Eighth";
            case 9: return "Nineth";
            case 10: return "Tenth";
            case 11: return "Eleventh";
            case 12: return "Twelth";
            case 13: return "Thirteenth";
            case 14: return "Fouteenth";
            case 15: return "Fifteenth";
            case 16: return "Sixteenth";
            case 17: return "Seventeenth";
            case 18: return "Eighteenth";
            case 19: return "Nineteenth";
            case 20: return "Twentieth";

            default: return "";
        }
    }

    string getDayName2(int val)
    {
        if (val == 30) return "Thirtieth";
        else if (val > 30) return "Thirty " + getDayName(val % 30);
        else if (val > 20) return "Twenty " + getDayName(val % 20);
        else return getDayName(val);
    }

    string makeCSV(string val)
    {
        string res = "";
        string[] ss = val.Split('\n');
        int curMonth = 0;
        for (int i = 0; i < ss.Length; i++)
        {
            if (ss[i].Trim() != "")
            {
                bool isInt = false;
                try
                {
                    int intA = Convert.ToInt32(ss[i][0].ToString());
                    isInt = true;
                }
                catch { }

                if (isInt)
                {
                    bool isDoubleInt = false;
                    try
                    {
                        int intB = Convert.ToInt32(ss[i][1].ToString());
                        isDoubleInt = true;
                    }
                    catch { }

                    int date = 0;
                    if (isDoubleInt) date = Convert.ToInt32(ss[i].Remove(2));
                    else date = Convert.ToInt32(ss[i][0].ToString());

                    DateTime dt = new DateTime(DateTime.Now.Year, curMonth, date);

                    string itemName = "";
                    if (isDoubleInt) itemName = ss[i].Substring(2);
                    else itemName = ss[i].Substring(1);

                    string itemName2 = ss[i + 1];
                    res += dt.DayOfWeek + " " + getMonthName(dt.Month) + " " + getDayName2(dt.Day) + " \"" + itemName + "\"" + " \"" + itemName2 + "\"\n";
                }
                else
                {
                    for (int j = 1; j < 13; j++)
                        if (ss[i].ToUpper().StartsWith(getMonthName(j)))
                        {
                            curMonth = j;
                            break;
                        }
                }
            }
        }
        return res;
    }
}

从文件流中使用 StreamReader.readToEnd(),获取字符串并使用该函数,或者如果文件很大,则逐行使用它。

评价如果这有帮助..

于 2013-09-04T14:51:16.810 回答
0

这会有点复杂。我遗漏了一些事情让你去做。

String[] months = { "January", "February", "March", ....}; 
Date processDate = new Date();
while (!reader.EndOfStream)
{
    string currentLine = reader.ReadLine();

    // skip this line if blank
    if (String.IsNullOrEmpty(currentLine)) continue;

    if (months.Contains(currentLine)) {
        // we have a new starting month.  
        // reset the process date
        Int32 month = DateTime.ParseExact(currentLine.Trim(), "MMMM", CultureInfo.CurrentCulture).Month;
        date = Convert.ToDate(month.ToString() + "/01/2013");
        continue;
    }


    // here's where the real fun begins:
    // you have to pull out the first two characters and test if one or both are digits.
    // This will give you the day.  Put that into your date variable.
    Int32 day = 0;
    char[] arr = currentLine.ToCharArray(0, currentLine.Length);
    if (Char.IsDigit(arr[1])) {
        // first two characters are numbers
        day = Convert.ToInt32(currentLine.Substring(0,2));
        currentLine = currentLine.Remove(0,2);
    } else {
        // only the first character is a number
        day = Convert.ToInt32(currentLine.Substring(0,1));
        currentLine = currentLine.Remove(0,1);
    }

    // set the new date
    date = new DateTime(date.Year, date.Month, day, 0, 0, 0);
    // If we can assume that ALL lines are broken into two parts then we can do the following:
    String secondLine = reader.ReadLine();
    currentLine = String.Format("{0} {1}", currentLine, secondLine);

    // At this point you have the month, day, and the entire line.
    // write it to your lunch stream or store in a StringBuilder 
}
于 2013-09-04T14:17:04.590 回答