1
$\begingroup$

I use this script to recognise a date in a textfile and write the result in another file.

For[i = 1, i < 20, i++,
  test1 = Import["E:\\data\\MC1 Data\\MC1 Data\\articles\\" <> ToString[i] <>".txt", "Text"];
  test2 = {StringCases[test1, DatePattern[{"Year", "Month", "Day"}]],
         StringCases[ToLowerCase[test1], RegularExpression["\\w(?<!\\d)[\\w'-]*"]]};
Write[s, test2]];

The result is a file like this:

example data file

test3 = ReadList["E:\\data\\MC1 Data\\MC1 Data\\articles\\testfile3.txt"]

Then I would like to make a selection on the date/first column. In this example, mathematica don't recognise it as a datastring. I tried to convert it like

DateString[test3[[All, 1]]]

but that didnt work. Who has a suggestion?

$\endgroup$
  • $\begingroup$ Please post the actual data so we can work with it rather than having to make it up. $\endgroup$ – Yves Klett Apr 22 '14 at 14:07
  • $\begingroup$ {{"2013/12/18"}, {"a", "b", "c"}} {{"1999/11/16"}, {"b", "c", "d"}} {{"1994/02/18"}, {"c", "d", "e"}} {{"1998/04/26"}, {"f", "g", "h"}} {{"1998/11/10"}, {"i", "j", "k"}} $\endgroup$ – Michiel van Mens Apr 22 '14 at 14:35
4
$\begingroup$

In order to work with the date you should convert it to a date list. Example:

    res = {{{"2013/12/8"}, {"a", "b"}}, {{"2013/12/8"}, {"a", "b"}}};
    res2 = MapAt[DateList@First@# &, res, {All, 1}]
(* Out: {{{2013, 12, 8, 0, 0, 0.}, {"a", "b"}}, {{2013, 12, 8, 0, 0, 
   0.}, {"a", "b"}}} *)

Now you can use Select or Cases or whatever to extract data. If you just want to use DateString on each date then you could do it like this: DateString /@ res2[[All, 1]].

$\endgroup$
  • $\begingroup$ Hi Pickett, Thanks for your reply. The file I used is very large so I import the file with: test3 = ReadList[ "E:\\data\\MC1 Data\\MC1 Data\\articles\\testfile3.txt"]; If I try next: ReadList[StringToStream[test3[[All, 1]]]]; the result is $Failed $\endgroup$ – Michiel van Mens Apr 22 '14 at 14:41
  • $\begingroup$ @MichielvanMens I updated my code to avoid confusion. I only used ReadList to make my example the same as yours. I don't think that part is necessary in order to answer the question, correct me if I'm wrong. After you've used ReadList you should have a list as in res in my example. $\endgroup$ – C. E. Apr 22 '14 at 14:54

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.