This is probably an easy question to answer, but I need a fresh eye... I'm struggling with processing the following file (extract shown here) It's a list of dates, and positive/negative time durations formatted using "m" and "s".

Apr-09 -1m 47s
Apr-11 -1m 15s
Apr-12 -0m 59s
Apr-13 -0m 44s
Apr-14 -0m 28s
Apr-15 -0m 13s
Apr-16 0m 1s
Apr-17 0m 15s
Apr-18 0m 29s
Apr-19 0m 42s
Apr-20 0m 55s
Apr-21 1m 8s

I'm reading the file in OK:

d = ReadList["/tmp/data.txt", 
  {Word, Number, Character, Number, Character}];

and assembling the pieces using this:

d /. {s_String, m_Integer, _, n_Integer, _} :> {s, 
   N@If[m >= 0, (60 m + n)/60, (60 m - n)/60]}  

which appears to work:

{"Apr-09", -1.78333}, 
{"Apr-11", -1.25},
{"Apr-12", 0.983333}, 
{"Apr-13", 0.733333}, 
{"Apr-14", 0.466667}, 
{"Apr-15", 0.216667},  
{"Apr-16", 0.0166667}, 
{"Apr-17", 0.25}, 
{"Apr-18", 0.483333},  
{"Apr-19", 0.7}, 
{"Apr-20", 0.916667}, 
{"Apr-21", 1.13333}

but then I noticed that the 0s lost their signs on import, so the conversion is inaccurate:

{{"Apr-09", -1, "m", 47, "s"}, 
 {"Apr-11", -1, "m", 15, "s"},
 {"Apr-12", 0, "m", 59, "s"}, 
 {"Apr-13", 0, "m", 44, "s"}, 
 {"Apr-14", 0, "m", 28, "s"},
 {"Apr-15", 0, "m", 13, "s"},
 {"Apr-16", 0, "m", 1, "s"},
 {"Apr-17", 0, "m", 15, "s"},
 {"Apr-18", 0, "m", 29, "s"}, 
 {"Apr-19", 0, "m", 42, "s"}, 
 {"Apr-20", 0, "m", 55, "s"},
 {"Apr-21", 1, "m", 8, "s"}}


The problem is that ReadList's Number reads "-0" as "0". Is there a better way of reading in data like this? I tried using Sign but unfortunately Sign[-0] gives 0... :)

  • $\begingroup$ Would you show the data as imported, before you manipulate it? I suspect that the number -0 is already read in as 0. If you read in "-0" as a string, you should be able to retain the minus sign. $\endgroup$
    – DavidC
    May 1 '13 at 12:37
  • $\begingroup$ @DavidCarraher Yes, the -0 is converted to 0 by ReadList. I could try reading as Word, I suppose. But the problem is then moved elsewhere rather than solved... $\endgroup$
    – cormullion
    May 1 '13 at 12:45

ReadList thinks your file has lists of 4, not 5, elements; it views the minutes and the "m" as a single element. The following will import the lists.

dd = ReadList["/tmp/data8.txt", {Word, Word, Number, Character}]

{{"Apr-09", "-1m", 47, "s"}, {"Apr-11", "-1m", 15, "s"}, {"Apr-12", "-0m", 59, "s"}, {"Apr-13", "-0m", 44, "s"}, {"Apr-14", "-0m", 28, "s"}, {"Apr-15", "-0m", 13, "s"}, {"Apr-16", "0m", 1, "s"}, {"Apr-17", "0m", 15, "s"}, {"Apr-18", "0m", 29, "s"}, {"Apr-19", "0m", 42, "s"}, {"Apr-20", "0m", 55, "s"}, {"Apr-21", "1m", 8, "s"}, {"Apr-22", "1m", 20, "s"}, {"Apr-23", "1m", 32, "s"}, {"Apr-24", "1m", 43, "s"}, {"Apr-25", "1m", 54, "s"}, {"Apr-26", "2m", 4, "s"}, {"Apr-27", "2m", 14, "s"}, {"Apr-28", "2m", 23, "s"}, {"Apr-29", "2m", 32, "s"}, {"Apr-30", "2m", 41, "s"}}

The following displays the output in minutes.

dd /. {s_, m_String, n_Integer, _} :> {s, (ToExpression[StringDrop[m, -1]]*60
+ (If[StringTake[m, 1] == "-", -1, 1]) n)/60}



  • $\begingroup$ Cool, thanks, once I've got the input in the right format, I'm there. I'm still stuck trying to get ReadList working for me to get to that point... $\endgroup$
    – cormullion
    May 1 '13 at 13:31
  • $\begingroup$ If you post a link to your data file, others may be able to help with the importing via ReadList. $\endgroup$
    – DavidC
    May 1 '13 at 14:23
  • $\begingroup$ The first gray box is an actual extract from the file, so it should be easy to save this in a text file. $\endgroup$
    – cormullion
    May 1 '13 at 14:36
  • $\begingroup$ The output in the first gray box is already transformed by Mathematica. Cutting and pasting it does not provide a list. I could try to guess how the data looked in the file form, but I prefer to review the actual file. $\endgroup$
    – DavidC
    May 1 '13 at 15:21
  • $\begingroup$ Thanks for sending the file. It turns out that ReadList parses the input differently from what I had imagined. (see second attempt, above.) $\endgroup$
    – DavidC
    May 1 '13 at 15:50

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.