10
$\begingroup$

I have a file with data in form {date, value}:
2010-05-19 17
2010-05-20 20
2010-05-21 19
...
How would I import such data in form {DateList, value}?

$\endgroup$
  • $\begingroup$ Kudos for using the ISO 8601 standard date format. The Fred Flintstone Format (MM/DD/YY) should be banned! :-) $\endgroup$ – stevenvh Oct 2 '12 at 10:16
  • $\begingroup$ @stevenvh as should be yards, inches and non-ISO units in general (my favorite is psi). $\endgroup$ – Yves Klett Oct 2 '12 at 10:57
13
$\begingroup$

Import[(* file *), "Table", "DateStringFormat" -> {"Year", "-", "Month", "-", "Day"}] seems to work...


As a test:

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

Import["test.dat", "Table",
       "DateStringFormat" -> {"Year", "-", "Month", "-", "Day"}]
{{{2010, 5, 19}, 17}, {{2010, 5, 20}, 20}, {{2010, 5, 21}, 19}}
| improve this answer | |
$\endgroup$
  • $\begingroup$ Beat me by a minute! $\endgroup$ – Szabolcs Jan 29 '12 at 10:21
  • $\begingroup$ Can you fix up yours to explicitly show that it works on the sample data? Then I'll delete mine. $\endgroup$ – Szabolcs Jan 29 '12 at 10:23
  • $\begingroup$ @J.M. It seems that no "-" is needed. Thanks. $\endgroup$ – enedene Jan 29 '12 at 10:28
  • $\begingroup$ @Szabolcs, Done. $\endgroup$ – J. M.'s discontentment Jan 29 '12 at 10:31
  • 3
    $\begingroup$ @enedene: I wanted to show how to indicate delimiters explicitly, should you encounter a more peculiar date format. Replace the dashes accordingly in the code above. $\endgroup$ – J. M.'s discontentment Jan 29 '12 at 10:33

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.