Here is an example:
Export["~/EncryptedAlt3/src/trading/ivv-01012017-03252018.csv",
FinancialData["NYSE:IVV", "Close", "Jan. 1, 2017"]]
The file contents then look like:
"{2017, 1, 3}",226.58
"{2017, 1, 4}",227.95
"{2017, 1, 5}",227.74
"{2017, 1, 6}",228.64
"{2017, 1, 9}",227.9
"{2017, 1, 10}",227.8
"{2017, 1, 11}",228.41
...
What I want is:
2017-01-03,226.58
I tried stuff like:
ds = SemanticImport[
"~/EncryptedAlt3/src/trading/ivv-01012017-03262018.csv"]
data = ds /. {d_String,
z__} :> {DateString[
DateList[d], {"Year", "-", "Month", "-", "Day"}], z}
Export["~/EncryptedAlt3/src/trading/ivv-01012017-03262018-tweaked.csv", data];
or
ds = SemanticImport[
"~/EncryptedAlt3/src/trading/ivv-01012017-03262018.csv"];
data = ds /. {d_String,
z__} :> {StringReplace[StringReplace[StringTrim[d, ("{" | "}")], "," -> "-"]," " -> ""], z}
Export["~/EncryptedAlt3/src/trading/ivv-01012017-03262018-tweaked.csv", data];
without success.
Update:
This one does it, but still leaves the quote marks on the date string which I don't want:
ds = Import[
"~/EncryptedAlt3/src/trading/ivv-01012017-03262018.csv", {"CSV",
"Dataset"}];
data = ds /. {d_String,
z__} :> {DateString[
ToExpression[d], {"Year", "-", "Month", "-", "Day"}], z};
Export["~/EncryptedAlt3/src/trading/ivv-01012017-03262018-tweaked.\
csv", data]
Result:
"2017-01-03",226.58
"2017-01-04",227.95
"2017-01-05",227.74
"2017-01-06",228.64
"2017-01-09",227.9
...
and I want
2017-01-03,226.58 ...
Update (with help from J.M.needshelp):
Just packaging things up from answer below into a function and add headers to the exported file.
Clear[getTickerData];
getTickerData[path_String, ticker_String, property_String, sinceDate_String, dateStringFormat_String] :=
Module[{raw},
raw = FinancialData[ticker, property, sinceDate];
Export[path, Prepend[MapAt[DateString[#, dateStringFormat] &, raw, {All, 1}] /. Missing[msg_] :> msg, {"Date", property}], "CSV", "TextDelimiters" -> ""]
];
getTickerData["~/EncryptedAlt3/src/trading/data/be:exx7-01012010-03272018.csv", "BE:EXX7", "Close", "Jan. 1, 2010", "ISODate"]
ExportString[]
only so that you can easily preview the expected results in Mathematica. Since your actual application exports to a file, do this:Export[path, MapAt[DateString[#, dateStringFormat] &, raw, {All, 1}] /. Missing[msg_] :> msg, "CSV", "TextDelimiters" -> ""]
. Adding the headers is a slight modification:Export[path, Prepend[MapAt[DateString[#, dateStringFormat] &, raw, {All, 1}] /. Missing[msg_] :> msg, {"Date", property}], "CSV", "TextDelimiters" -> ""]
. $\endgroup$