Reading Text File and Parsing Lines

2,205 views
Skip to first unread message

EB

unread,
Dec 16, 2009, 9:10:40 AM12/16/09
to golang-nuts
Hi,

Could someone please point me to an example in the documentation or
show me a code sample
how to read in text file and parse each input line consisting of space
delimited values such as:

101 John Doe 12-12-1950
102 Jane Doe 11-10-1951
...

Where first column is numeric ID, second and third are strings (for
names) and last column is date.

I would like to read in each line and tokenize it/splice it into the
respective fields.

Thank you in advance.

Qtvali

unread,
Dec 16, 2009, 9:52:57 AM12/16/09
to golang-nuts
There is a scanner in pkg/go/scanner/scanner.go.

You should pay special attention to the following:

// Read the next Unicode char into S.ch.
// S.ch < 0 means end-of-file.
//
func (S *Scanner) next() {
if S.offset < len(S.src) {
S.pos.Offset = S.offset;
S.pos.Column++;
r, w := int(S.src[S.offset]), 1;
switch {
case r == '\n':
S.pos.Line++;
S.pos.Column = 0;
case r >= 0x80:
// not ASCII
r, w = utf8.DecodeRune(S.src[S.offset:len(S.src)])
}
S.offset += w;
S.ch = r;
} else {
S.pos.Offset = len(S.src);
S.ch = -1; // eof
}
}


You might also want to use "expect" method, which calls errorHandler
if next char is not expected, also there are isLetter and isDigit
declared; scanMantissa is the simplest scanner example, which will go
through all digits on given constraint - input is "base" variable, if
you set it to 10, then it goes over all digits 0-9 and returns (next
scan command will start from where this one finished). You can use
S.pos afterwards to create a slice.

In case the whole thing is too much, I think that this function here
does the job - go through a line with it, then use slice and do with
the resulting string slice what you want. You must release the string
if you don't want to keep whole file hidden in memory afterwards,
slice is a string pointer.

EB

unread,
Dec 16, 2009, 10:57:58 AM12/16/09
to golang-nuts
Thanks.

Is there some simpler example?

I am wanting to do something that would in Java (slightly abbreviated
and without error checking)
in the simplest form looked something like:

BufferedReader in = new BufferedReader(new FileReader("test.txt"));

StringBuilder line = new StringBuilder( 5000 );

while ( ( line = in.readLine() ) != null ) ){

String[] fields = line.split( " " )'

int id = Integer.ParseInt( fields[ 0 ] );
String fname = fields[ 1 ];
String lname = fields[ 2 ];
DateFormat df = DateFormat.getDateInstance();
Date date = df.parse( fields[ 4 ] );



}//end while


Thank you in advance.
Message has been deleted

Michael Hoisie

unread,
Dec 16, 2009, 4:18:15 PM12/16/09
to golang-nuts
This is a small example that takes parses an ini file line-by-line:
http://github.com/hoisie/web.go/blob/master/ini.go

It uses strings.Split to extract the lines of a file.

- Mike

On Dec 16, 9:05 am, Qtvali <qtv...@gmail.com> wrote:
> http://golang.org/pkg/strings/- there is split defined
Reply all
Reply to author
Forward
0 new messages