Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
Please post the actual data so we can work with it rather than having to make it up. –  Yves Klett Apr 22 at 14:07
    
{{"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"}} –  Michiel van Mens Apr 22 at 14:35

1 Answer 1

up vote 3 down vote accepted

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]].

share|improve this answer
    
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 –  Michiel van Mens Apr 22 at 14:41
    
@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. –  Pickett Apr 22 at 14:54
    
Thanks! It Works fine –  Michiel van Mens Apr 22 at 17:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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