Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?
share|improve this question
add comment

2 Answers

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.

share|improve this answer
2  
+1. InputForm rules! –  FJRA Apr 30 '12 at 19:14
    
The "CSV" option is an elegant solution! thanks ..( & thanks for the InputForm gyaan ) –  my account_ram Apr 30 '12 at 22:11
add comment

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"]]
share|improve this answer
    
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) –  TomD Apr 30 '12 at 19:05
    
@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. –  tkott Apr 30 '12 at 19:47
    
@ no input format called "Data": thanks! ( confused with "Data" data in a generic Mathematica form (list, string, etc.) in the documentation –  my account_ram Apr 30 '12 at 22:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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