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.

This might be a very simple question but I have been struggling with it for a couple of hours, so thought maybe someone here can show me a quick way of solving it.

I have a file in rtf format (Triangles.rtf) https://drive.google.com/file/d/0B_DoKsn6afPMRTNHUW93NEEwc1U/edit?usp=sharing

Now I am trying to load the file by typing a = Import["PATH_TO_FILE/Triangles.rtf","Table"] which seems to return a list of lists. So far it seems fine, for example a[[1]] returns {3 0 2 1}. Head[a[[1]]] returns list and Head[a[[1,1]]] returns String. Then I try to convert string into number by ToExpression[a] but here we get a list of Null!

can anyone please tell me how to convert my imported data into a 2d array (list of list that is) so that I can use the numerics?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

While I completely agree with Szabolcs on this. I also enjoy playing with file formats being a theoretical chemist. So here is how you would read this specific file:

  readYourFile[file_String] := Module[{str = OpenRead[file], data},
  ReadList[str, String, 5];
  data = ToExpression @ ReadList[str, Table[Record, {4}], 
     RecordSeparators -> {"\\", " ", "\n", "f0", "fs24", "cf0", "}"}];
  Close[str]; data]

Usage:

 data = readYourFile["Triangles.rtf"]

Now you have your numerical data.

{{3, 0, 2, 1}, {3, 3, 5, 4}, {3, 6, 8, 7}, {3, 9, 11, 10}, {3, 12, 14,
   13}, {3, 15, 17, 16}, {3, 18, 20, 19}, {3, 21, 23, 
  22}, <<265434>>, {3, 796326, 796328, 796327}, {3, 796329, 796331, 
  796330}, {3, 796332, 796334, 796333}, {3, 796335, 796337, 
  796336}, {3, 796338, 796340, 796339}, {3, 796341, 796343, 
  796342}, {3, 796344, 796346, 796345}, {3, 796347, 796349, 796348}}

Let's Check:

MatrixQ[data, NumberQ]

True

share|improve this answer

I am not saying that it is not possible to load data formatted like this from an RTF file using Mathematica, but the proper answer here is really: do not use RTF (Rich Text Format) files to store such data! Just convert the file to a plain text file (txt) using your favourite word processor (WordPad on Windows, TextEdit on Mac) and load the plain text file as a Table. It'll work fine.

RTF files are designed for storing text, including formatting, and are simply not suitable for storing numerical data.

share|improve this answer
5  
And there I was thinking that this was some kind of special Real Triangle Format file used by trigonometricians - didn't occur to me it was good old Rich Text-Format... –  cormullion Oct 30 '13 at 19:29

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.