[rust-dev] read file line by line

Mic mictadlo at gmail.com
Tue Apr 3 06:54:38 PDT 2012


Thank you. How to check whether the last line is not empty?
Because line.split_char('\t') would not make sense to run on an empty line.

In python I did it in the following way:
        with open(args.output, 'r') as outfile:
            for line in infile:
                try:
                    parts = [part.strip() for part in line.split('\t')]
                except IndexError:
                    continue

On Tue, Apr 3, 2012 at 8:36 PM, Mohd. Bilal Husain <bilal at bilalhusain.com>wrote:

> As you figured out, the function read_line can be used from the
> reader_util implementation from module io
>
> ~~~~
>
> import io::reader_util;
>
> #[doc = "reads the entire file line by line except the first line"]
> fn main(args: [str]) {
>     if args.len() == 1u {
>         fail #fmt("usage: %s <filename>", args[0]);
>     }
>
>     let r = io::file_reader(args[1]); // r is result<reader, err_str>
>     if result::failure(r) {
>         fail result::get_err(r);
>     }
>
>     let rdr = result::get(r);
>     rdr.read_line(); // skip line
>     while !rdr.eof() {
>         io::println(rdr.read_line());
>     }
> }
> ~~~~
>
> I don't think Rust lets you catch exceptions while reading the stream as
> you can't do much about it*.
>
> * Error handling in Rust is unrecoverable unwinding
>
> On 3 April 2012 13:34, Mic <mictadlo at gmail.com> wrote:
>
>> Hello,
>> I found read_line, but I do not how to convert the following Python code
>> (skip first line and print all other lines from a file) to Rust.
>>
>> f = open(file_name, 'r')
>> f.next()                        #skip line
>> for line in f:
>>     print line
>> f.close()
>>
>> How rust handle exceptions?
>>
>> Thank you in advance.
>>
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev at mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120403/8e62eb49/attachment.html>


More information about the Rust-dev mailing list