MathGroup Archive 2011

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

Search the Archive

Re: read CSV

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117788] Re: read CSV
  • From: David <dlkeith at comcast.net>
  • Date: Thu, 31 Mar 2011 04:03:35 -0500 (EST)
  • References: <imus7l$i3t$1@smc.vnet.net>

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"

Hello Alan,

You can do this using Import to read the data into a list structure,
and then work with the list structure to extract and format the data
as you please.

I copied your data and made it into a text file. I then used Import to
read the text file, forcing CSV format, and extracted the XY column
data.

Below is a notebook expression. Copy the notebook expression as text.
The open a new Mathematica notebook and paste the expression into it.
Say yes to interpret the notebook. When you execute the notebook it
will open a dialog box allowing you to select your data file. It will
then import the file and process it.

Kind regards,

David

-----------------------------------------------

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}}]
}, 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}]
}, 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}]
}, 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[{"raw", ",", " ",
    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}}],

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}]
}, 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}}]
}, 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}}]
}, 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: read CSV
  • Next by Date: an example... Re: Why Mathematica does not issue a warning when the calculations
  • Previous by thread: Re: read CSV
  • Next by thread: Re: read CSV