开发者

Problem importing csv file : ABC.csv file in mathematica 8

I have a csv file Nifty_PE :

01-Dec-2008,       11.76,
02-Dec-2008,       11.65,
03-Dec-2008,       11.64,
04-Dec-2008,       12.22,
05-Dec-2008,       11.90,
08-Dec-2008,       12.20,
10-Dec-2008,       12.84,
11-Dec-2008,       12.80,
12-Dec-2008,       12.81,
15-Dec-2008,       13.07,
16-Dec-2008,       13.33,

When i give the following cmd in mathematica :

Take[Import["C:\\Users\\ROHAN\Desktop\\NIFTY_PE.csv", "CSV"], 5]

I get the output as :

{{"01-Dec-2008", 11.76, ""}, {"02-Dec-2008", 11.65, 
  ""}, {"03-Dec-2008", 开发者_如何学Python11.64, ""}, {"04-Dec-2008", 12.22, 
  ""}, {"05-Dec-2008", 11.9, ""}}

What i desire to get is :

{{"01-Dec-2008", 11.76}, {"02-Dec-2008",...........

Kindly help me what i should do to get the desired output..


Try this instead:

Import["C:\\Users\\ROHAN\Desktop\\NIFTY_PE.csv", "CSV"][[1;;5, 1;;2]]

[[1;;5, 1;;2]] indexes only a part of the returned array. 1;;5 means rows 1 through 5. 1;;2 means columns 1 and 2 -- ignoring the empty third column. If you want to keep all rows instead of just the first five, use [[All, 1;;2]].

All of this is documented under the function Part.


Alternatively, you could do something like

Take[Import["C:\\Users\\ROHAN\Desktop\\NIFTY_PE.csv", "CSV"], 5, 2]

which would take the first 2 columns of the first 5 rows of the imported table.


Alternatively *2, if you don't know (or don't wish to check in advance) the desired dimensions of your matrix you can use a replacement rule.

Import["C:\\Users\\ROHAN\Desktop\\NIFTY_PE.csv", "CSV"] /. "" -> Sequence[]

Or if you are concerned that there might also be blanks somewhere other than at the end of a row, and you only want to eliminate the ones at the end of the row:

Import["C:\\Users\\ROHAN\Desktop\\NIFTY_PE.csv", "CSV"] /. {a__,""} -> {a}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜