The @io module contains functions and classes related to standard/file input/output and manipulating various text file formats.

@io Functions

:load(path) #

Load file at path into string.

Parameters

Return

:save(path,data) #

Save data to file at path.

Parameters

Return

:prompt(q) #

Prompt for user input.

Parameters

Return

Example

f = io.prompt('Degrees in F? ').number() c = (f-32)*5/9 print '=> ',c,'degrees in C' ## Degrees in F? 99 => 37.2222 degrees in C ## :status(index,total,message) #

Print a status message. Multiple calls will re-use the same line. This is meant to be called within a loop to give progress feedback to the user.

Parameters

Notes

The output is limited to 79 characters, so that it will fit one line of a terminal 80 characters wide.

This function prints to stderr.

At the last step (index+1 == total) a newline is printed.

Example

for n,1000 io.status(n,1000,'step: '+n) os.sleep(10) print 'Done.' #while running: #[203/1000] (20.3%) step: 202 #when done: #[1000/1000] (100.0%) step: 999 #Done. :exists(path) #

Determine whether or not file exists.

Parameters

Return

:remove(path) #

Remove (delete) file at path.

Parameters

Return

:rename(old_name,new_name) #

Rename file from old_name to new_name.

Parameters

Return

@file Methods

:new(path,flags) #

Construct a stream to a file.

Parameters

Return

Notes

The flags can by any of the following:

The underlying implementation uses fopen().

:read(n) #

Read n bytes from the stream.

Parameters

Return

:write(s) #

Write bytes s to stream.

Parameters

Return

Notes

The return value is equal to s.len() unless there is an error.

:seek(pos) #

Seek stream to a position within the file.

Parameters

Return

:read_line() #

Read a single line from the file.

Return

Notes

The character '\r' is ignored for cross-platform compatibility.

Example

f = io.file('bigfile.txt') while (line = f.read_line())!=null print 'LINE:',line

@csv Functions

:load(path) #

Load CSV (comma-separate values) file at path.

Parameters

Return

:save(path,data) #

Save data to CSV file at path.

Parameters

Return

:parse(s) #

Parse string as CSV.

Parameters

Return

:escape(value) #

Escape a single CSV value

Parameters

Return

Notes

If value contains commas or newlines, it is quoted and quotes within are replaced with two quotes. If value contains no commas or newlines, nothing is done.

:split_line(line) #

Split a single line of CSV.

Parameters

Return

Notes

This is useful if the CSV file is too big to read into memory at once.

Example

f = io.file('bigfile.csv') while (line = f.read_line())!=null print 'VALUES:',io.csv.split_line(line)

@html Functions

:parse_table(html) #

Parse HTML table.

Parameters

Return

Notes

Only <tr> and <td> tags are parsed. This does fuzzy parsing for maximum compatibility.

:get_text(html) #

Extract text from HTML.

Parameters

Return

Notes

This is similar to .textContent in Javascript.

@ini Functions

:load(path) #

Load .ini/.conf/.cfg file.

Parameters

Return

Notes

Comments can be indicated with either ';' or '#'.

Each subsection in the .ini file becomes a subobject.

Any whitespace around the '=' is ignored. On any line, leading or trailing whitespace is ignored.

Value can be JSON literals. If values failed to parse as JSON, they are parsed as strings.

Example

#test.ini: ## ;this is a comment #also a comment asdf = narf local = false port = 1234 [narf] asdf = [1,2,3] zxcv = 3 ## print io.ini.load('test.ini') ## { "asdf" : "narf", "local" : false, "port" : 1234, "narf" : { "asdf" : [1,2,3], "zxcv" : 3 } } ##

@json Functions

:load(path) #

Load a JSON file.

Parameters

Return

Notes

This is equivalent to io.load(path).parse_json().

JSON serialization and parsing are part of the core library, and therefore not repeated here. See :parse_json() and :json().

:save(path,data) #

Save data to JSON file at path.

Parameters

Notes

This is equivalent to io.save(path,data.json()).