简体   繁体   中英

Combining 2 .csv Files as well as sorting the content by date with different time format

I am currently working on a task where i want to take the content of 2 .csv files and put them in one new .csv file as well as sorting the content by date.

so far so good... the problem is that the content uses several datetime formats

can one of you guys help me with this?

here ist the code i have so far

    //reading the raw files
    string[] rawdata = File.ReadAllLines(PathInRaw);
    string[] rawdataTick = File.ReadAllLines(PathInRawTick);


    //clearing existing file and writing in the new content
    File.WriteAllText(PathOut, " ");
    File.AppendAllLines(PathOut, rawdata);
    File.AppendAllLines(PathOut, rawdataTick);


    //changing date format??? which i dont get to work
    string[] list = { };
    int counter = 0;
    foreach (string line in File.ReadAllLines(PathOut))
    {
        column = line.Split(';');
        column[0] = DateTime.Now.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
        list[counter] = Convert.ToString(column);
        counter++;
    }
    File.WriteAllText(PathOut, " ");
    File.WriteAllLines(PathOut, list);


    //sorting it
    DateTime d = DateTime.MinValue;
    var query = from line in File.ReadLines(PathOut)
                let fields = line.Split(';')
                let dateParsed = DateTime.TryParse(fields[0], out d)
                let dateObject = dateParsed ? d : DateTime.MinValue
                orderby dateParsed, dateObject
                select line;

    List<string> sortedLines = query.ToList();
    File.WriteAllText(PathOut, " ");
    File.WriteAllLines(PathOut, sortedLines);

The Dateformates I have in the .csv are
5/30/2018 2:48:57 PM (MM/dd/yyyy HH:mm:ss a) 06.01.2018 06:12:19 (MM.dd.yyyy HH:mm:ss) 20180601 16:21:50 (yyyyMMdd HH:mm:ss)

The first two formats should be standard enough to be parsed with DateTime.Parse(...) . The last one is a bit too custom for this to catch, so implementing a parsing method will be your best bet.

private static DateTime ParseDateTime(string dateTime) {
    DateTime? d1 = null;
    try {
        d1 = DateTime.Parse(dateTime);
    }
    catch { }

    if (d1 == null) {
        try {
            d1 = DateTime.ParseExact(dateTime, "yyyyMMdd HH:mm:ss", CultureInfo.InvariantCulture);
        }
        catch { }
    }

    return (DateTime)d1;
}

Here the first try catch block will catch all the standard formats and if it does not, the d1 object will still be null so then we will try your custom format. Finally we cast back to a non-nullable DateTime object to make it easier to work with.

A way to use this would be the following, where you would replace the two hard coded lists of DateTime strings with reading from your files.

List<string> rawLines1 = new List<string>() { "5/30/2018 2:48:57 PM", "06.01.2018 06:12:19" };
List<string> rawLines2 = new List<string>() { "20180601 16:21:50" };

List<string> rawLines = new List<string>();
rawLines.AddRange(rawLines1);
rawLines.AddRange(rawLines2);

List<DateTime> parsedDateTimes = new List<DateTime>();
foreach (string rawLine in rawLines) {
    parsedDateTimes.Add(ParseDateTime(rawLine));
}

parsedDateTimes = parsedDateTimes.OrderBy(x => x).ToList();

The very last line takes care of sorting back into the order with the oldest at the top. If you want this reversed, replace .OrderBy(x => x) with .OrderByDescending(x => x)

From here you can write back out to your output file.

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