1
$\begingroup$

Hi I am new to Mathematica. I have a large text file that looks like: {"i":"2008-02-22","v":"9.102"},{"i":"2008-02-23","v":"11.14"},{"i":"2008-02-24","v":"12.21"}... where the "i" value is a date (YYYY-MM-DD) and the "v" is a number. I need to import this into a Datelist object. I have tried using Import and ReadList with no success. Any help would be appreciated.

$\endgroup$
  • 1
    $\begingroup$ Although I am sure that, with enough effort, one can import this file as is, it may be much easier to do some pre-cleaning outside of Mathematica. It would be easy, for instance, to remove the "i": and "v": parts and the curly braces, and to turn the comma into a new line; the resulting file should then be easy to import. $\endgroup$ – MarcoB Feb 22 at 20:04
  • $\begingroup$ Thanks MarcoB. I did think of that, but I have around 1000 of these files so I was hoping for something more programmatic, but maybe I can clean them in python first. $\endgroup$ – mkrueger Feb 22 at 21:01
1
$\begingroup$

Might be faster to do it using sed rather than Python. e.g.

sed -e 's/"i":\|"v"://g; s/},/}\n/g; s/{\|}//g'

Alternative to @MarcoB's answer, probably faster than using Interpreter:

stream = StringToStream[
   "{\"i\":\"2008-02-22\",\"v\":\"9.102\"},{\"i\":\"2008-02-23\",\"v\"\
:\"11.14\"},{\"i\":\"2008-02-24\",\"v\":\"12.21\"}"];

string = Import[stream, "String"];

list = "{" <> StringReplace[string, {"\"i\":" -> "", "\"v\":" -> ""}] <> "}" // ToExpression;

dateList = {DateObject[First[#]], ToExpression[Last[#]]} & /@ list;

DateListPlot[dateList]

enter image description here

$\endgroup$
  • $\begingroup$ This is perfect. Thank you so much. $\endgroup$ – mkrueger Feb 25 at 20:58
  • $\begingroup$ @mkrueger You are welcome. If you don't mind, could you accept my answer. $\endgroup$ – Rohit Namjoshi Feb 25 at 21:43
0
$\begingroup$
data = ImportString[
  "{\"i\":\"2008-02-22\",\"v\":\"9.102\"},{\"i\":\"2008-02-23\",\"v\":\
    \"11.14\"},{\"i\":\"2008-02-24\",\"v\":\"12.21\"}" ];

StringReplace[
  data,
  "{\"i\":\"" ~~ Shortest[date__] ~~ "\",\"v\":\"" ~~ Shortest[val__] ~~ "\"}" :> {date, val}
] // Cases[_List]

(* Out: 
{{"2008-02-22", "9.102"}, {"2008-02-23", "11.14"}, {"2008-02-24", "12.21"}}
*)

Then use an Interpreter object, primed with the correct format:

{Interpreter["Date"][#1], Interpreter["Number"][#2]} & @@@ listofdata

(* Out:
{{DateObject[{2008, 2, 22}, "Day", "Gregorian", -6.], 9.102},
 {DateObject[{2008, 2, 23}, "Day", "Gregorian", -6.], 11.14},
 {DateObject[{2008, 2, 24}, "Day", "Gregorian", -6.], 12.21}}
*)

note: Interpreter is sslloww!

$\endgroup$

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.