# Importing .txt or .csv financial data

I am trying to import some financial data exported from Excel. You can find the relevant file here : Text File or CSV File

This is what the data looks like in Excel :

Using the following code to import :

BankShadowFed =
"Table"];


I have the following problem:

While I can Change to expression each element,

I can't get it all right including the dates...

What is the right way to Import[] those files ? Or Export[] them in the first place?

-
What do you get instead? –  rcollyer Feb 15 '12 at 16:53
@ian.milligan I approved your edit, but I don't think it is helpful to add [] brackets to function names. –  Mr.Wizard Feb 15 '12 at 16:53
@Mr.Wizard Good to know, thanks for the heads up - will keep that in mind in the future. –  programming_historian Feb 15 '12 at 16:57
@rcollyer, instead of ? The Problem is the Numbers are yet string. I tried all solution offered in my question on plotting list with e. unsuccessfully. –  500 Feb 15 '12 at 17:06
This looks like the same date format that was covered in stackoverflow.com/questions/8896674. Szabolcs's answer looks particularly useful. –  Brett Champion Feb 15 '12 at 17:33

Overall, your data is just badly formatted. For instance, later in the list your dates look similar to "3-Mar" which is interpreted as the third of March not March 2003, as you intended. For the most part, this is not your fault, but Excel arbitrarily formats data, and you have to be vigilant that it doesn't misinterpret it. Towards that end, I've rewritten my answer to work around the misformatting.

First things first, your numeric data is formatted with commas, so we need to get rid of them.

killCommas[a_String] := StringReplace[a, "," -> ""]
killCommas[a_] := a


Second, your dates, as I said above, are misformatted. The code, below, splits the date at "-", checks if the numeric part has length 1 and prepends it with a "0" if it does, sorts the two strings by length, reverses the sort, and puts them back together with "-" between.

fixDate[a_String] := StringJoin@
Riffle[Reverse[
If[StringLength[#] == 1, "0" <> #, #] & /@
SortBy[StringSplit[a, "-"], StringLength]],
"-"]


Now, I strip off the header from the list

dat = Import[<txt url>, "TABLE"][[2 ;;]]


and using the helper functions, I fix all of the data:

{DateList[{fixDate[#1], {"MonthNameShort", "YearShort"}}],
Sequence @@ ToExpression[ killCommas /@ {##2}]} & @@@ dat

-
Nice and concise. The comma problem also exists in the second column just the example choosen had only a 3 digit number but if you look further down the list you see the 4 digit numbers appearing. –  Matariki Feb 15 '12 at 18:56
@Matariki you're right. My testing only included the first few points. Corrected. –  rcollyer Feb 15 '12 at 19:03
@rcollyer, did your solution work for you ? I am sorry, but I still cant get it right using your solution. –  500 Feb 15 '12 at 23:16
@500, I fixed it. –  rcollyer Feb 16 '12 at 0:49
@rcollyer, Sadly, I think I will keep you updated :-) –  500 Feb 16 '12 at 2:29

Assuming I understand correctly your question, here's what I would do: First, let's assign the second column to a variable a=BankShadowFed[[All,2]]. Now let's remove every , from the numbers in the list, using aa=StringReplace[a, "," -> ""].

We have a list of strings, which we can now turn into a list of expressions using aaa=Table[ToExpression[aa[[i]]], {i, 2, Length[aa]}]. This can now be plotted using ListPlot[aaa]` to give

Of course, you can do this for every column, and re-create a table from scratch. I hope this was useful. If not, precise your question!

-