4
$\begingroup$

I have been trying to change a list of 2D coordinates with square brackets to curly brackets so I can plot the set of points in Mathematica. However, it does not plot because it puts quotation marks around each numeric value. Why is it doing this, and what should I change? The original .csv file looks like [x1, y1],[x2, y2],... and I want {{x1,y1},{x2,y2},...}

My attempt:

list = Import["C:\\Users\Kenny\Python\AngleTop.csv"];
listy = Flatten[list, 1];
flat1 = StringDelete[listy, "["];
flat2 = StringDelete[flat1, "]"];
flat3 = StringDelete[flat2, " "];
party = Partition[flat3, 2];
DensityHistogram[party]
$\endgroup$
5
$\begingroup$

{x,y,...} is just syntactic sugar for List[x,y,...].

You can add List to every pair and wrap all of them on a big list.

(* copied from Jason B's answer *)
csvData = "[8,18],[0,17],[6,8],[3,4],[1,4],[12,6],[13,3],[18,8],[5,10],[8,13],[8,13],[13,9],[10,4],[12,5],[4,5],[2,16],[20,16],[8,7],[8,16],[1,0]"

ToExpression["{" <> StringReplace[csvData, {"[" -> "List["}] <> "}"]

{{8, 18}, {0, 17}, {6, 8}, {3, 4}, {1, 4}, {12, 6}, {13, 3}, {18, 8}, {5, 10}, {8, 13}, {8, 13}, {13, 9}, {10, 4}, {12, 5}, {4, 5}, {2, 16}, {20, 16}, {8, 7}, {8, 16}, {1, 0}}}

$\endgroup$
3
$\begingroup$
foo = ReadList[StringToStream[#], Record, RecordSeparators -> {{"["}, {"]"}}] /. 
     x_String :> ToExpression["{" ~~ x ~~ "}"] &;

example1 = "[x1,y1],[x2,y2],[x3,y3]";
example2 = 
  "[8,18],[0,17],[6,8],[3,4],[1,4],[12,6],[13,3],[18,8],[5,10],[8,13],
[8,13],[13,9],[10,4],[12,5],[4,5],[2,16],[20,16],[8,7],[8,16],[1,0]";

foo@example1

{{x1, y1}, {x2, y2}, {x3, y3}}

foo@example2

{{8, 18}, {0, 17}, {6, 8}, {3, 4}, {1, 4}, {12, 6}, {13, 3}, {18, 8}, {5, 10}, {8, 13}, {8, 13}, {13, 9}, {10, 4}, {12, 5}, {4, 5}, {2, 16}, {20, 16}, {8, 7}, {8, 16}, {1, 0}}

$\endgroup$
2
$\begingroup$
f[string_] := 
 Partition[
  ToExpression /@ 
   StringSplit[StringReplace[string, {"[" -> "", "]" -> ""}], ","], 2]

f["[x1,y1],[x2,y2]"]

(*
{{x1, y1}, {x2, y2}}
*)
$\endgroup$
2
$\begingroup$

Your method works just fine, except at the end the elements of party are strings, {{"x1","y1"},...}. So just change that line to read

party = Partition[ToExpression /@ flat3, 2];

Another method, similar to the answer above, but different in that I read the whole file in as one string and work on it. If I have this text file,

[8,18],[0,17],[6,8],[3,4],[1,4],[12,6],[13,3],[18,8],[5,10],[8,13],[8,13],[13,9],[10,4],[12,5],[4,5],[2,16],[20,16],[8,7],[8,16],[1,0]

saved as test.csv, then this works,

ToExpression["{" <> StringReplace[
   Import["test.csv", "Text"], {"[" -> "{", "]" -> "}"}] <> "}"]
(* {{8, 18}, {0, 17}, {6, 8}, {3, 4}, {1, 4}, {12, 6}, {13, 
  3}, {18, 8}, {5, 10}, {8, 13}, {8, 13}, {13, 9}, {10, 4}, {12, 
  5}, {4, 5}, {2, 16}, {20, 16}, {8, 7}, {8, 16}, {1, 0}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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