# Exctrating data information from a file.txt

I really did previous long search in the site and I couldn't find any answer that might help me. So I am asking it.

I have the following data set, https://onedrive.live.com/redir?resid=4BEAF2395C9CC198%215058 (this is only a very small part, as a sample ok?), so I tried the following codes ,that i copy from the forum:

 $1 = OpenRead[ "C:\\Users\\decicco\\SkyDrive\\Documentos\\ProjetoFinal\\Simbad\\Teste_\ dataMining.txt"];$2 = ReadList[$1, String]; Close[$1];

Flatten@StringCases[$2, "Coordinates(Gal,ep=J2000,eq=2000): " ~~ (x : NumberString ...) ~~ (y : ___ ~~ NumberString ...) -> ToExpression@{x, y}] Out[64]= {"307.0804", " +06.8343 ", "307.7283", " 10.4014 "} Paralaxes erro e qualidade In[125]:= Flatten@StringCases[$2,
"Parallax: " ~~ (x : ___ ~~ NumberString ...) ~~
"[" ~~ (y : ___ ~~ NumberString ...) ~~ "]" ~~ (z : _?LetterQ ...) ->
ToExpression@{x, y, z}]

Out[125]= {"0.65 ", "0.44", "", "0.95 ", "0.36", ""}

Tipo Espectral

Flatten@StringCases[$2, "Spectral type: " ~~ x : ___ -> x] Out[108]= {"B0.5Ia C ~ ", "B2.5Ib C ~ "} Identificadores de catálogos = Flatten@StringCases[$2,
RegularExpression["(?m)^Identifiers "] ~~ "(" ~~ DigitCharacter ~~
") :" ~~ __ ~~ RegularExpression["(?m)^Notes "]]

Out[146]= {}


So as you can see from the outputs they are not very well done.

What I need:

As you can see in the link, each star is marked like that: Object HR 5027 ---, and the nomenclature HR 5027 is the star that I need the informations, Galactic positions, Parallax, Spectral type and Identifiers. As I can do a list or table like that, or similar:

{{Star 1 , Galactic Coordinate ->......, ->.........;Paralaxes-> ...., error-> ...., quality-> (a letter can be A, B or C);Identifiers-> ....,.....,......, etc. },{Star 2 , Galactic Coordinate ->......, ->.........;Paralaxes-> ...., error-> ...., quality-> (a letter can be A, B or C);Identifiers-> ....,.....,......, etc. }]

I really do not know about associations (<....->....>), and I do not know if is the case here. Is there any good tutorial tha I can learn about associations( as I have always to work with large database with many subitens)?

26/12/2014:

I did some progress: -TAKING THE COORDINATES, TWO FOR EACH STAR

    Flatten@StringCases[$2, "Coordinates(Gal,ep=J2000,eq=2000): " ~~ (x : NumberString ...) ~~ (y : ___ ~~ NumberString ...) -> ToExpression@{x, y}] {"307.0804", " +06.8343 ", "307.7283", " 10.4014 "}  -TAKING PARALAX , ERROR AND QUALITY FOR EACH STARS  Flatten@StringCases[$2,
"Parallax: " ~~ (x : ___ ~~ NumberString ...) ~~
"[" ~~ (y : ___ ~~ NumberString ...) ~~ "]" ~~
RegularExpression["\\s"] ~~ (z : WordCharacter ...) ->
ToExpression@{x, y, z}]

{"0.65 ", "0.44", "A", "0.95 ", "0.36", "A"}


-TAKING THE SPECTRAL TYPES :

 Flatten@
StringCases[$2, "Spectral type: " ~~ x : Except["~"] ... -> x] Out[21]= {"B0.5Ia C ", "B2.5Ib C "}  but, my tries for getting identifiers for each stars did not work: Flatten@ StringCases[$2,
"Identifiers" ~~ RegularExpression["\\s+"] ~~ "(" ~~
DigitCharacter ... ~~ "):" ~~ WordBoundary ~~
x : LetterCharacter ... ~~ WordBoundary ~~ "Notes" :> x]

Out[121]= {}

• I am not sure if I understood your problem right: seeking a solution to extract certain information stored in a text file. I.e. a kind of pattern search in the text file? Dec 26 '14 at 17:38
• Yes, exactly, I have a big .txt, contaninig informations about stars, and I need to get the coordinates, paralax (including the error measuremnts) spectral types and finally Identifiers Dec 26 '14 at 19:37

O.K. I give it a try.... I have saved your data in a file "stars.txt" on my computer. Now I do the following (... yes... a brute force attack...) ;-) Here a step by step approach...

in = Import["stars.txt", "Words"]; (* Import the textfile* )
tempi = StringPosition[in,  "Identifiers"] ; (*Position of "Identifieres" *)
pi = Position[(Length /@ tempi), 1] // Flatten (*get the position of "Identifiers" *)
tempn =  StringPosition[in, "Notes"] ; (* same for "Notes" *)
pn = Position[(Length /@ tempn), 1] // Flatten;
take = {pi, pn}\[Transpose] ;(* build the "cut-off" coordinates *)
cut = in[[#[[1]] ;; #[[2]]]] & /@ take; (* cut of desired areas of the list of words *)
cut = Drop[#, 2] & /@ cut; (* Drop "Identifiers, (30):" and so on  *)
Drop[#, -1] & /@ cut (* Drop the word "Notes"*)


Now one has the area between "Identifieres" and "Notes" as a list of words and can process these. I´m still not sure whether this might help you (and I´m not familiar with star-positions and so on... ). But now you can build pairs of this list an so get the desired information, put it in a Dataset or what else.

• Thanks for the help. BUt This code separates the letter an number, will not work. Well the identifiers are organized in columns, so Is there a way to get each element, from the row of each column? Dec 27 '14 at 12:02
• You can get the same organization of the data, quite easily. Name the last output (the Drop... Line) data and then: data = Partition[#,2]& /@ data and finally Map[neat,data,{2}] where neat[{x_,y_}]:=StringJoin[x," ",y] and the identifiers are as in the text file. Dec 27 '14 at 13:32
• Yes, mgmamer, it worked fine!! Thanks!! Dec 27 '14 at 13:52

One approach is to import the data and then parse it using Mathematica's string commands. To get you started, here is the import command and a search for the locations where the word "Object" appears:

q = Import["file.txt"];
StringPosition[q, "Object"]


You can replace "Object" with whatever word you want (like "Coordinates") and find where in the input string the various words are. There are many other commands you might find useful for parsing text, including 'StringSplit' and 'StringReplace'. You can find a list of the commands that start with String using

?String*

• Hi I know the commands, I think your approach not so adequate as the parametres repeat at each star from the data.txt. What I need is a code that I can get the coordinates, paralaxes, spectral type ana identifiers, for each star and consolidated them one big dataset or association. And do the same operations for other catalogues, anda finally join the big datasets catalogues in only one Data set, that contains the various catalogues with its own stars + parameters.The sample that I put here, it isform one specific catalogue, but there are many more. Dec 26 '14 at 20:32
• Then you need to find something that defines where the data is in the input: I was suggesting that you might calibrate it with respect to words. You could also try position (how many characters/words in succession). There must be some structure in the input, this is what you mist exploit. Dec 26 '14 at 21:28