Management Presentations. It's extremely difficult for us to present our findings if we are restricted to PDF, which can only show static images, and the CDF just changes that instantly. Even better, it creates a unique eBook that lets you actually interact with the figures on the page through dynamic computation. All rights reserved. Enable JavaScript to interact with content and submit forms on Wolfram websites.
For example, you may have data generated by an external program which consists of a sequence of numbers separated by spaces. This data cannot be read directly as Wolfram Language input. However, the function ReadList can take such data from a file or input stream, and convert it to a Wolfram Language list. ReadList [ " file " , Number ] read a sequence of numbers from a file , and put them in a Wolfram Language list.
This reads all the numbers in the file, and returns a list of them:. This puts each successive pair of numbers from the file into a separate list:. This makes each line in the file into a separate list:. Thus, for example, ReadList will read 2. Note that ReadList can handle numbers with any number of digits of precision.
ReadList can handle numbers in this form:. ReadList [ " file " , type ] read a sequence of objects of a particular type. ReadList can read not only numbers, but also a variety of other types of object.
Each type of object is specified by a symbol such as Number. Here are the integer codes corresponding to each of the bytes in the file:. This puts the data from each line in the file into a separate list:. Byte single byte of data , returned as an integer. This returns a list of the "words" in the file strings :. ReadList allows you to read "words" from a file. It considers a "word" to be any sequence of characters delimited by word separators.
You can set the option WordSeparators to specify the strings you want to treat as word separators. The default is to include spaces and tabs, but not to include, for example, standard punctuation characters. Note that in all cases successive words can be separated by any number of word separators. These separators are never taken to be part of the actual words returned by ReadList. Options for ReadList. This reads the text in the file strings as a sequence of words, using the letter e and.
The Wolfram Language considers any data file to consist of a sequence of records. By default, each line is considered to be a separate record. In general, you can set the option RecordSeparators to give a list of separators for records. Note that words can never cross record separators. As with word separators, any number of record separators can exist between successive records, and these separators are not considered to be part of the records themselves.
By default, each line of the file is considered to be a record:. Here is a file containing three "sentences" ending with periods:. This allows both periods and newlines as record separators:. This puts the words in each "sentence" into a separate list:. Settings for the RecordSeparators option. Here is a file containing some text:. This reads all the text in the file source and returns it as a single string:. This gives a list of the parts of the file that lie between : and : separators:.
By choosing appropriate separators, you can pick out specific parts of files:. The Wolfram Language usually allows any number of appropriate separators to appear between successive records or words. Sometimes, however, when several separators are present, you may want to assume that a "null record" or "null word" appears between each pair of adjacent separators. Here is a file containing "words" separated by colons:. Here the repeated colons are treated as single separators:. Now repeated colons are taken to have null words in between:.
In most cases, you want words to be delimited by separators that are not themselves considered as words. Sometimes, however, it is convenient to allow words to be delimited by special "token words", which are themselves words. You can give a list of such token words as a setting for the option TokenWords. This reads the text, using the specified token words to delimit words in the text:. You can use ReadList to read Wolfram Language expressions from files.
In general, each expression must end with a newline, although a single expression may go on for several lines. Here is a file containing text that can be used as Wolfram Language input:. This reads the text in exprs as Wolfram Language expressions:. This prevents the expressions from being evaluated:.
ReadList can insert the objects it reads into any Wolfram Language expression. The second argument to ReadList can consist of any expression containing symbols such as Number and Word specifying objects to read. If ReadList reaches the end of your file before it has finished reading a particular set of objects you have asked for, then it inserts the special symbol EndOfFile in place of the objects it has not yet read. The symbol EndOfFile appears in place of numbers that were needed after the end of the file was reached:.
ReadList [ "! This executes the Unix command date , and reads its output as a string:. OpenRead [ " file " ] open a file for reading. Functions for reading from input streams. ReadList allows you to read all the data in a particular file or input stream. Sometimes, however, you want to get data a piece at a time, perhaps doing tests to find out what kind of data to expect next.
When you read individual pieces of data from a file, the Wolfram Language always remembers the "current point" that you are at in the file. When you call OpenRead , the Wolfram Language sets up an input stream from a file, and makes your current point the beginning of the file.
Every time you read an object from the file using Read , the Wolfram Language sets your current point to be just after the object you have read. Using Skip , you can advance the current point past a sequence of objects without actually reading the objects. This opens an input stream from the file:. This reads the first number from the file:.
This reads the second pair of numbers:. Note that if you try to read past the end of file, Read returns the symbol EndOfFile. FindList [ " file " , " text " ] get a list of all the lines in the file that contain the specified text.
Finding lines that contain specified text. This returns a list of all the lines in the file containing the text is :. The text fourth appears nowhere in the file:. By default, FindList scans successive lines of a file, and returns those lines which contain the text you specify. In general, however, you can get FindList to scan successive records , and return complete records which contain specified text. As in ReadList , the option RecordSeparators allows you to tell the Wolfram Language what strings you want to consider as record separators.
Note that by giving a pair of lists as the setting for RecordSeparators , you can specify different left and right separators. By doing this, you can make FindList search only for text which is between specific pairs of separators.
This finds all "sentences" ending with a period which contain And :. Options for FindList. This finds only the occurrence of Here which is at the beginning of a line in the file:. In general, FindList finds text that appears anywhere inside a record. The option WordSeparators specifies the list of separators for words. The text th does appear in the file, but not as a word.
As a result, the FindList fails:. This searches for third in two copies of textfile :. It is often useful to call FindList on lists of files generated by functions such as FileNames. FindList [ "! Finding text in the output from an external program. This runs the external Unix command date in a text-based interface:.
FindList works by making one pass through a particular file, looking for occurrences of the text you specify. Sometimes, however, you may want to search incrementally for successive occurrences of a piece of text. You can do this using Find. In order to use Find , you first explicitly have to open an input stream using OpenRead.
Then, every time you call Find on this stream, it will search for the text you specify, and make the current point in the file be just after the record it finds. As a result, you can call Find several times to find successive pieces of text.
This opens an input stream for textfile :. This finds the first line containing And :. Calling Find again gives you the next line containing And :. Once you have an input stream, you can mix calls to Find , Skip , and Read. This finds the first line which contains second , and leaves the current point in the file at the beginning of the next line:. Read can then read the word that appears at the beginning of the line:.
This skips over the next three words:. The Wolfram Language finds is in the remaining text, and prints the entire record as output:. StreamPosition [ stream ] find the position of the current point in an open stream.
Finding and setting the current point in a stream. Functions like Read , Skip , and Find usually operate on streams in an entirely sequential fashion. Each time one of the functions is called, the current point in the stream moves on. Sometimes, you may need to know where the current point in a stream is, and be able to reset it.
On most computer systems, StreamPosition returns the position of the current point as an integer giving the number of bytes from the beginning of the stream. When you first open the file, the current point is at the beginning, and StreamPosition returns 0 :.
This reads the first line in the file:. Now Read returns the remainder of the first line:. Functions like Read and Find are most often used for processing text and data from external files. In some cases, however, you may find it convenient to use these same functions to process strings within the Wolfram Language. You can do this by using the function StringToStream , which opens an input stream that takes characters not from an external file, but instead from a Wolfram Language string.
StringToStream [ " string " ] open an input stream for reading from a string. This opens an input stream for reading from the string:. This reads the first "word" from the string:. This reads the remaining words from the string:. Input streams associated with strings work just like those with files. At any given time, there is a current position in the stream, which advances when you use functions like Read.
The current position is given as the number of characters from the beginning of the string by the function StreamPosition [ stream ]. You can explicitly set the current position using SetStreamPosition [ stream , n ]. Here is an input stream associated with a string:. The current position is initially 0 characters from the beginning of the string:. The current position is now 3 characters from the beginning of the string:. This sets the current position to be 1 character from the beginning of the string:.
If you now read a number from the string, you get the 23 part of :. This sets the current position to the end of the string:. If you now try to read from the stream, you will always get EndOfFile :. Particularly when you are processing large volumes of textual data, it is common to read fairly long strings into the Wolfram Language, then to use StringToStream to allow further processing of these strings within the Wolfram Language. Once you have created an input stream using StringToStream , you can read and search the string using any of the functions discussed for files.
This puts the whole contents of textfile into a string:. This opens an input stream for the string:. This gives the lines of text in the string that contain is :. This resets the current position back to the beginning of the string:. This finds the first occurrence of the in the string, and leaves the current point just after it:. This reads the "word" which appears immediately after the :.
Functions like Read and Write handle ordinary printable text. But in dealing with external data files or devices it is sometimes necessary to go to a lower level, and work directly with raw binary data. Wolfram Universal Deployment System Instant deployment across cloud, desktop, mobile, and more. Wolfram Knowledgebase Curated computable knowledge powering Wolfram Alpha. Answer Unmark. Mark as an Answer. Import and Export Wolfram Language.
Posted 4 years ago. Follow this post. Mark as an Answer Reply Flag. Gustavo Delfino. Hi Delfino, I have attached my input text file posit.
Wolfram Science Technology-enabling science of the computational universe. Wolfram Notebooks The preeminent environment for any technical workflows. Wolfram Engine Software engine implementing the Wolfram Language. Wolfram Data Framework Semantic framework for real-world data. Wolfram Universal Deployment System Instant deployment across cloud, desktop, mobile, and more.
0コメント