MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: read CSV

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117794] Re: read CSV
  • From: David <dlkeith at comcast.net>
  • Date: Thu, 31 Mar 2011 04:04:41 -0500 (EST)

On Mar 30, 2:13 am, Alan <alan.is... at gmail.com> wrote:
> I've received some files formatted like below.
> How can I read selected columns into a list?
> I'm coming from a background where I'd read in each line,
> discarding the first, then (in this case at least)
> split on the comma, and coerce the type.  So I'm happy
> to do that if I can figure out how.  (Naturally I could
> preprocess the data into a form that say ReadList
> likes better, but I'm looking for a pure Mathematica solution.)
>
> Thanks,
> Alan Isaac
>
> "x","y","color","pen down?"
> "0","0.26161459854014585","105","true"
> "1","0.2665251497682828","105","true"
> "2","0.29244385081680907","105","true"
> "3","0.31995416628471895","105","true"

Sorry -- my earlier reply included a typo -- here is a corrected
notebook:

-----------

Notebook[{

Cell[CellGroupData[{
Cell["Choose the file using a dialog", "Subsection",
 CellChangeTimes->{{3.510498132049691*^9, 3.510498154778931*^9}}],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"file", "==",
  RowBox[{
  "SystemDialogInput", "[", "\"\<FileOpen\>\"", "]"}]}]], "Input",
 CellChangeTimes->{{3.510497968155803*^9, 3.5104980119450803`*^9}}],

Cell[BoxData["\<\"C:\\\\Users\\\\David\\\\Documents\\\\Tools\\\\\
Mathematica\\\\read\\\\data.txt\"\>"], "Output",
 CellChangeTimes->{{3.510498006703471*^9, 3.510498022787099*^9},
   3.510509780051465*^9, 3.5105098459615803`*^9}]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Set the directory to that of the selected file", "Subsection",
 CellChangeTimes->{{3.510498158819338*^9, 3.5104981918757963`*^9}}],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"SetDirectory", "[",
  RowBox[{"DirectoryName", "[", "file", "]"}], "]"}]], "Input",
 CellChangeTimes->{{3.510498027108307*^9, 3.5104980559839573`*^9}}],

Cell[BoxData["\<\"C:\\\\Users\\\\David\\\\Documents\\\\Tools\\\\\
Mathematica\\\\read\"\>"], "Output",
 CellChangeTimes->{3.51049805759076*^9, 3.5105097802230654`*^9,
  3.510509846164381*^9}]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Import the file as a CSV", "Subsection",
 CellChangeTimes->{{3.510498201516613*^9, 3.5104982177406416`*^9}}],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"raw1", "==",
  RowBox[{"Import", "[",
   RowBox[{"file", ",", "\"\<CSV\>\""}], "]"}]}]], "Input",
 CellChangeTimes->{{3.510498065453174*^9, 3.510498079383999*^9}, {
   3.510498523516779*^9, 3.510498526917585*^9}, {
   3.5104985870424905`*^9, 3.5104985881812925`*^9},
   3.5104986942770786`*^9}],

Cell[BoxData[
 RowBox[{"{",
  RowBox[{
   RowBox[{"{",
    RowBox[{"\<\"x\"\>", ",", "\<\"y\"\>", ",", "\<\"color\"\>",
     ",", "\<\"\\\"pen down?\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "0", ",", "0.26161459854014585`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "1", ",", "0.2665251497682828`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "2", ",", "0.29244385081680907`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "3", ",", "0.31995416628471895`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}], ",",
   RowBox[{"{", "\<\"\"\>", "}"}], ",",
   RowBox[{"{", "\<\"\"\>", "}"}], ",",
   RowBox[{"{", "\<\"\"\>", "}"}], ",",
   RowBox[{"{", "\<\"\"\>", "}"}]}], "}"}]], "Output",
 CellChangeTimes->{
  3.5104980844072075`*^9, 3.510498527853586*^9, {3.510498562737648*^9,
    3.5104985900844955`*^9}, 3.5104986954470806`*^9,
   3.5105097804570656`*^9, 3.5105098463671813`*^9}]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Select all records with length 4", "Subsection",
 CellChangeTimes->{{3.5104987265535355`*^9, 3.5104987458819695`*^9}, {
  3.5104989656159573`*^9, 3.510498975849575*^9}}],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"raw2", "==",
  RowBox[{"Select", "[",
   RowBox[{"raw1", ",", " ",
    RowBox[{
     RowBox[{
      RowBox[{"Length", "[", "#", "]"}], "\[Equal]", "4"}], "&"}]}],
   "]"}], " ",
  RowBox[{"(*", " ",
   RowBox[{"read", " ", "about", " ", "pure", " ", "functions"}], " ",
    "*)"}]}]], "Input",
 CellChangeTimes->{{3.5104986985826864`*^9, 3.510498721140326*^9}, {
   3.5104987519035797`*^9, 3.5104987663336053`*^9},
   3.5105098024999046`*^9}],

Cell[BoxData[
 RowBox[{"{",
  RowBox[{
   RowBox[{"{",
    RowBox[{"\<\"x\"\>", ",", "\<\"y\"\>", ",", "\<\"color\"\>",
     ",", "\<\"\\\"pen down?\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "0", ",", "0.26161459854014585`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "1", ",", "0.2665251497682828`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "2", ",", "0.29244385081680907`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{
    "3", ",", "0.31995416628471895`", ",", "105",
     ",", "\<\"\\\"true\\\" \"\>"}], "}"}]}], "}"}]], "Output",
 CellChangeTimes->{
  3.510498721920327*^9, {3.5105097805662656`*^9,
   3.510509804715108*^9}, 3.510509846382781*^9}]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Select rows 2 through the last and the XY columns only", \
"Subsection",
 CellChangeTimes->{{3.510498237474676*^9, 3.510498300498787*^9}, {
  3.510498775678022*^9, 3.5104987838836365`*^9}, {
  3.5104988263469105`*^9, 3.5104988476877484`*^9}}],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"data", "==",
  RowBox[{"raw2", "[",
   RowBox[{"[",
    RowBox[{
     RowBox[{"2", ";;"}], ",",
     RowBox[{"{",
      RowBox[{"1", ",", "2"}], "}"}]}], "]"}], "]"}], "  ",
  RowBox[{"(*", " ",
   RowBox[{
   "look", " ", "up", " ", "Part", " ", "to", " ", "see", " ", "what",
     " ", "this", " ", "means"}], " ", "*)"}]}]], "Input",
 CellChangeTimes->{{3.5104983063175974`*^9, 3.5104983592640905`*^9}, {
  3.5104984602274675`*^9, 3.5104984635502734`*^9}, {
  3.5104986662438297`*^9, 3.5104986664310303`*^9}, {
  3.510498792198451*^9, 3.5104988078140783`*^9}}],

Cell[BoxData[
 RowBox[{"{",
  RowBox[{
   RowBox[{"{",
    RowBox[{"0", ",", "0.26161459854014585`"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{"1", ",", "0.2665251497682828`"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{"2", ",", "0.29244385081680907`"}], "}"}], ",",
   RowBox[{"{",
    RowBox[{"3", ",", "0.31995416628471895`"}], "}"}]}],
  "}"}]], "Output",
 CellChangeTimes->{
  3.5104983620252953`*^9, 3.5104984642054744`*^9,
   3.5104986160897417`*^9, 3.5104986673202314`*^9, {
   3.510498786754041*^9, 3.510498808375679*^9},
   3.5105097806598663`*^9, 3.51050981154792*^9, 3.510509846398381*^9}]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["Plot the strokes", "Subsection",
 CellChangeTimes->{{3.5104988800734053`*^9, 3.5104988889654207`*^9}}],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{"ListLinePlot", "[", "data", "]"}]], "Input",
 CellChangeTimes->{{3.510498861134972*^9, 3.5104988735837936`*^9}}],

Cell[BoxData[
 GraphicsBox[{{}, {},
   {Hue[0.67, 0.6, 0.6],
    LineBox[{{0., 0.26161459854014585`}, {1., 0.2665251497682828}, {
     2., 0.29244385081680907`}, {3., 0.31995416628471895`}}]}},
  AspectRatio->NCache[GoldenRatio^(-1), 0.6180339887498948],
  Axes->True,
  AxesOrigin->{0, 0.262},
  PlotRange->{{0., 3.}, {0.26161459854014585`, 0.31995416628471895`}},
  PlotRangeClipping->True,
  PlotRangePadding->{
    Scaled[0.02],
    Scaled[0.02]}]], "Output",
 CellChangeTimes->{{3.510498867530983*^9, 3.5104988755337973`*^9},
   3.510509780722266*^9, 3.5105098149175262`*^9,
   3.5105098464295816`*^9}]
}, Open  ]]
}, Open  ]]
},
WindowSize->{707, 647},
WindowMargins->{{24, Automatic}, {Automatic, 30}},
FrontEndVersion->"8.0 for Microsoft Windows (64-bit) (February 23, \
2011)",
StyleDefinitions->"Default.nb"
]


  • Prev by Date: Re: Why Mathematica does not issue a warning when the calculations
  • Next by Date: Re: read CSV
  • Previous by thread: Re: read CSV
  • Next by thread: Re: read CSV