9
$\begingroup$

I tried the following commands

ImportString[
  "129,160,16,4
  130,160,16,4", "Data"] // FullForm
(* Out= List[List["129,160,16,4"],List["130,160,16,4"]] *)

ImportString[
  "129,160,16,4
  130,160,16,4
  131,160,16,4
  132,160,16,4", "Data"] // FullForm

(* Out= List[List[129,160,16,4],List[130,160,16,4],List[131,160,16,4],List[132,160,16,4]] *)

Note that in the first case, the output is a list of strings, whereas in the second case the output is a list of list of numbers.

  • Why is this so ?
  • Can I force the first case to return integers as in the second case?
$\endgroup$
0

2 Answers 2

10
$\begingroup$

I have no idea why the first ImportString brings things in as strings, but if you import as "CSV" instead, it works as you want.

In[22]:= ImportString[
  "129,160,16,4
  130,160,14,5", "CSV"] // InputForm
Out[22]= {{129, 160, 16, 4}, {130, 160, 14, 5}}

You can use InputForm to force the display of quote marks; you don't need the much more verbose FullForm.

$\endgroup$
1
  • $\begingroup$ The "CSV" option is an elegant solution! thanks ..( & thanks for the InputForm gyaan ) $\endgroup$ Apr 30, 2012 at 22:11
10
$\begingroup$

I think the problem is that there is no input format called "Data":

In[253]:= MemberQ[$ImportFormats, "Data"]

Out[253]= False

If I use "Table":

In[248]:= ImportString["129,160,16,4
130,160,16,4
131,160,16,4
132,160,16,4","Table"]//FullForm

Out[248]//FullForm=List[List["129,160,16,4"],List["130,160,16,4"],List["131,160,16,4"],List["132,160,16,4"]]
$\endgroup$
3
  • $\begingroup$ I am sure you are right, but why does ImportString["129,160,16,4 130,160,16,4", "Elements"] give me {"Data", "Grid"}? ("Grid" seems to work OK) $\endgroup$
    – user1066
    Apr 30, 2012 at 19:05
  • $\begingroup$ @TomD Well, ImportString is just a specific Import wrapper, so Import is being called to import the "Data" from text string in the "default data format" (whatever that may be in this particular case). My guess is that the "default data format" changes between the two strings due to some internal algorithm. $\endgroup$
    – tkott
    Apr 30, 2012 at 19:47
  • $\begingroup$ @ no input format called "Data": thanks! ( confused with "Data" data in a generic Mathematica form (list, string, etc.) in the documentation $\endgroup$ Apr 30, 2012 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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