0
$\begingroup$

I'm trying to import a .csv file that contains some points which are geo-positions, but the import gives me the points wrapped in quotes. Is there any method to get them as pairs of real numbers?

I'm getting "{26.2417,-98.432},{26.2407,-98.4247}" but I want {26.2417,-98.432},{26.2407,-98.4247}.


Update

Here is an example that produces output with the problem:

csvstring = "\"{26.2417,-98.432},{26.2407,-98.4247}\",2\n\"{26.2417,-98.432},{26.2407,-98.4247}\",3"; 
ImportString[csvstring, "CSV"]

{{"{26.2417,-98.432},{26.2407,-98.4247}", 2},
 {"{26.2417,-98.432},{26.2407,-98.4247}", 3}} 
$\endgroup$
  • 2
    $\begingroup$ You could use ToExpression on your lists. Otherwise, without the CSV file and the code used to Import, it will be difficult to provide a solution. $\endgroup$ – Yves Klett Jul 20 '15 at 9:41
  • $\begingroup$ ToExpression::sntx: Invalid syntax in or before "{26.2417,-98.43201},{26.2407,-98.42469000000001}". I got this error $\endgroup$ – Alberto Jul 20 '15 at 10:02
  • 1
    $\begingroup$ Okay, can you please provide the *.csv file you have, or a sample of it if it's too large? $\endgroup$ – J. M. will be back soon Jul 20 '15 at 10:06
  • 2
    $\begingroup$ ... because your pasted expression structure is invalid (lacks enclosing {}). Without more details from your side anything more detailed will be a waste of time. $\endgroup$ – Yves Klett Jul 20 '15 at 10:07
  • $\begingroup$ I-2,"{26.2417,-98.43201},{26.2407,-98.42469000000001}" that's the csv file, but you're right i miss lacks enclosing thanks $\endgroup$ – Alberto Jul 20 '15 at 10:10
5
$\begingroup$

Here are two ways. SemanticInterpretation is sort of fun but extremely slow:

SemanticInterpretation["{26.2417,-98.432},{26.2407,-98.4247}", List[_, _] ..]

Fixing up your data to fit Yves Klett's suggestion is much faster:

ToExpression["{" <> "{26.2417,-98.432},{26.2407,-98.4247}" <> "}"]

Both yield

(*  {{26.2417`, -98.432`}, {26.2407`, -98.4247`}}  *)

If there were a bunch of them with the same format, then this will replace all of them:

import = {"{26.2417,-98.432},{26.2407,-98.4247}"};
import /. s_String /; 
   StringMatchQ[s, RegularExpression["{[0-9.-]*,[0-9.-]*},{[0-9.-]*,[0-9.-]*}"]] :> 
  ToExpression["{" <> s <> "}"]

Depending on what's in the CSV file, one might alter the RegularExpression. Conceivably, this might work:

RegularExpression["{.*}"]

Note: A problem with the data are the commas separating the coordinates, which appear to be stored as a single item in the CSV file. Hence the commas are "escaped" by being contained by quotes in the CSV file, and the CSV interpreter returns a string. I don't know of a way to influence the CSV interpreter in the way one can SemanticInterpretation, so I think some sort of post-processing is probably necessary.

$\endgroup$
  • $\begingroup$ You always make me feel bad coming up with these interesting and helpful answers after I voted to close (mind you, the blame is all mine!). $\endgroup$ – Yves Klett Jul 20 '15 at 15:35
1
$\begingroup$

Without having the .csv file to work on, as guess who it is said, this should get you where your going.

cord=Import["mydrive/file.csv","Data"]//ToExpression

Again Please give a sample of what the data is in the .csv file.

$\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.