2
$\begingroup$

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"]
$\endgroup$
4
  • $\begingroup$ Do you want "2017-01-03,226.58" as a String? $\endgroup$ Mar 26, 2018 at 22:32
  • $\begingroup$ @alancalvitti, Yes, I want that written to the export file as a string so that in the exported file it is simply data with no quotes, but yes you have the format correct. $\endgroup$
    – Joe
    Mar 26, 2018 at 22:57
  • 1
    $\begingroup$ A note: Hans used 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$ Mar 27, 2018 at 1:55
  • $\begingroup$ @J.M. thanks for revisiting this, your modification works great. $\endgroup$
    – Joe
    Mar 28, 2018 at 15:40

1 Answer 1

4
$\begingroup$

Try With @J.M. suggestion/comment of much cleaner code

  raw = FinancialData["NYSE:IVV", "Close", "Jan. 1, 2017"];
 ExportString[MapAt[DateString[#, "ISODate"] &, raw, {All, 1}] /. Missing[msg_] :> msg, "CSV", "TextDelimiters" -> ""]

Below are just preliminary post

 ExportString[
 Map[Function[
   StringJoin[DateString[#[[1]], "ISODate" ], ",", 
    ToString[#[[2]] ]]],  
  FinancialData["NYSE:IVV", "Close", "Jan. 1, 2017"]], "Text"]

Using ExportString to get immediate output. Change to Export[yourfilenamehere, ...] if this meets your needs

This also works

    ExportString[
 Map[Function[{DateString[#[[1]], "ISODate" ], ToString[#[[2]] ]}],  
  FinancialData["NYSE:IVV", "Close", "Jan. 1, 2017"]], "CSV", 
 "TextDelimiters" -> ""]
$\endgroup$
8
  • 1
    $\begingroup$ Just raw = FinancialData["NYSE:IVV", "Close", "Jan. 1, 2017"]; ExportString[MapAt[DateString[#, "ISODate"] &, raw, {All, 1}], "CSV", "TextDelimiters" -> ""] suffices. $\endgroup$ Mar 26, 2018 at 23:44
  • 1
    $\begingroup$ @J.M. True Dat. Mostly pointing out ISODate. Don't recall which version it was introduced. Your version is cleaner. My overkill on the Close value was due to Missing["NotAvailable"] value might popup for some financial data. $\endgroup$
    – Hans
    Mar 27, 2018 at 0:03
  • $\begingroup$ @J.M. that is what I wanted. Make it an answer if you like and I'll accept it. Thanks to both of you. $\endgroup$
    – Joe
    Mar 27, 2018 at 0:08
  • 1
    $\begingroup$ @Joe, please accept Hans's answer instead, contingent on him editing his answer. $\endgroup$ Mar 27, 2018 at 0:09
  • 1
    $\begingroup$ @Hans, I forgot to account for Missing[], thanks for reminding! I guess something like ExportString[MapAt[DateString[#, "ISODate"] &, raw, {All, 1}] /. Missing[msg_] :> msg, "CSV", "TextDelimiters" -> ""] should take care of it. $\endgroup$ Mar 27, 2018 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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