Mathematica: how to read files?

In summary, there are two .txt files, map.txt and event.txt. The map.txt file contains data in {} brackets, while the event.txt file contains data separated by | and commas. To read these files in Mathematica, you can use the OpenRead function and the RegularExpression function to extract the desired data.
  • #1
nenyan
67
0
reading data from txt files
there are two .txt files. One is map.txt, another is event.txt.

map looks like below:

dfafdafskdjf:{{0.837711763427 -0.8359730469 2.4152891949e-04 0.000360245716236} {0.831761777387 -0.8240730748 2.4446745066e-04 0.000359914513716} {0.819861805217 -0.8062231165 2.4910805126e-04 0.000359863615611}} klsajlkf

What I need are data in {}, omited the begging srting and the endding string.


event.txt:
PGC | Name | RA | Dec | Type | App_Mag | Maj_Diam (a) | err_Maj_Diam | Min_Diam (b) | err_Min_Diam | b/a | err_b/a | PA | Abs_Mag | Dist | err_Dist | err_App_Mag | err_Abs_Mag |

2|UGC12889|0.00047|47.27450|3.1|13.31|1.546|0.498|1.314|~|0.85|0.100|~|-21.05|72.458|10.869|0.61|0.61|
4|PGC000004|0.00096|23.08764|5.0|15.39|0.851|0.078|0.186|~|0.219|0.015|~|-18.68|63.264|13.918|0.39|0.40|
...
...


How to read these two files by mathematica? Thanks.
 
Physics news on Phys.org
  • #2
Perhaps this will get you started

In[1]:= stream=OpenRead["map.txt"];
allwords="";
While[(word=Read[stream,Record])=!=EndOfFile,
word=StringReplace[word,RegularExpression["[a-z:]*"]->""];
allwords=StringJoin[allwords,word];
];
Close[stream];
Print[allwords];

From In[1]:=
{{0.837711763427 -0.8359730469 2.4152891949-04 0.000360245716236} \
{0.831761777387 -0.8240730748 2.4446745066-04 \
0.000359914513716}{0.819861805217 -0.8062231165 2.4910805126-04 \
0.000359863615611}}

Now you will need to position your map.txt file in the appropriate place or you will have to give a path to the OpenRead.
Notice this has "eaten" your 'e' in your exponential form because the regular expression is eliminating alphabetic characters, and colons. But you would have needed to find a way to correctly translate your exponents into a Mathematica format anyway and this will just give you a little more practice. Notice you do not have commas between your }{ in your input and this will make it a little more difficult to have Mathematica easily translate this as a single simple expression using ToExpression.

This should give you an idea how to get started. Look up RegularExpression in Mathematica for more information. If you get stuck then show how much you have figured out and just where you are stuck and someone can provide more pointers.
 

1. How do I import data from a file into Mathematica?

To import data from a file into Mathematica, you can use the "Import" function. This function allows you to specify the file path and format of the file you want to import. For example, if you have a CSV file called "data.csv" located in the same directory as your Mathematica notebook, you can use the following command: Import["data.csv", "CSV"]. This will import the data from the file and store it in a variable for further use in your code.

2. Can Mathematica read data from different file formats?

Yes, Mathematica has the capability to read data from various file formats such as CSV, Excel, JSON, XML, and more. You can use the "Import" function and specify the format of your file to import data from different file formats.

3. How do I handle missing or incorrect data when reading files in Mathematica?

Mathematica has built-in functions for handling missing or incorrect data when reading files. One option is to use the "MissingDataRules" option in the "Import" function, which allows you to specify how missing or incorrect data should be handled. Alternatively, you can use the "DeleteMissing" function to remove any rows or columns with missing data from your imported dataset.

4. Can Mathematica read files from a remote location or URL?

Yes, Mathematica has the ability to import data from remote locations or URLs. You can use the "Import" function and specify the URL of the file you want to import. You can also use the "URLRead" function to download the file first and then import it using the "Import" function.

5. Is it possible to read only a specific part of a large file in Mathematica?

Yes, you can use the "SkipLines" and "Take" options in the "Import" function to read only a specific part of a large file. The "SkipLines" option allows you to skip a certain number of lines from the beginning of the file, while the "Take" option allows you to specify the number of lines or rows to be read. This can be useful when working with large datasets to save time and memory.

Back
Top