# Import date and number Matrix Formatted as text

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.

• 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. – MarcoB Feb 22 '19 at 20:04
• 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. – mkrueger Feb 22 '19 at 21:01

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]


• This is perfect. Thank you so much. – mkrueger Feb 25 '19 at 20:58
• @mkrueger You are welcome. If you don't mind, could you accept my answer. – Rohit Namjoshi Feb 25 '19 at 21:43
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!