5
$\begingroup$

Based on an example from here I try to parse dates from a table including the time, but it fails. This is the modified example:

Export["test.dat", {{"2010-05-19 22:23:43", 17}, {"2010-05-20 22:23:43", 20}, {"2010-05-21 22:23:43", 19}}, "FieldSeparators" -> " "];

Import["test.dat", "Table", "DateStringFormat" -> {"Year", "-", "Month", "-", "Day", " ", "Hour", ":", "Minute", ":", "Second"}]

Does anyone understand why it doesn't work in analogy to the other case?

thanks

$\endgroup$
  • $\begingroup$ "DateStringFormat" seems to only return three elements (Y,M,D ) even if (Y,M,D H:M:S) are specified and apparently recognised. $\endgroup$ – image_doctor Jul 6 '12 at 8:42
7
$\begingroup$

A workaround:

imported = Import["test.dat", "Table"]

We get:

(* {{"2010-05-19 22:23:43", 17}, {"2010-05-20 22:23:43", 
    20}, {"2010-05-21 22:23:43", 19}} *)

You can then apply DateList to convert it to a date list:

MapAt[DateList, #, 1] & /@ imported

(* {{{2010, 5, 19, 22, 23, 43.}, 17}, {{2010, 5, 20, 22, 23, 43.}, 
    20}, {{2010, 5, 21, 22, 23, 43.}, 19}} *)
$\endgroup$
  • 1
    $\begingroup$ Even shorter: MapAt[DateList, #, 1] & /@ imported $\endgroup$ – Mr.Wizard Jul 6 '12 at 10:21
  • $\begingroup$ @Mr.Wizard you're right, I'm going to correct it. $\endgroup$ – VLC Jul 6 '12 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.