You are not logged in.

#1 2006-02-24 14:15:16

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Python equivalent to Perl's "$/"?

Hi Python gurus,

In perl, it's possible to rewrite the global special variable $/ (aka $INPUT_RECORD_SEPARATOR or $RS).

By default this is set to the newline char. Do, if your reading an input stream,  like while(<>) then each item is a line.

However, it's useful (for me, at least) to redefine this to a different string, which for a given task means I can read blocks of lines together that are in fact related as a single record.

Anyway, you know what I'm getting at. In Python, I often write:

for line in open(filename):
  # do something with line

Does any one know of a way to redefine the delimiter here in Python, or do I really have to go about writing additional buffer code?

Cheers

Offline

#2 2006-02-24 16:15:00

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Python equivalent to Perl's "$/"?

er.... I'd just read the whole line and use string.split() (think that's the syntax) to split it into a list and process on that. If that's what you're asking. I think the delimiter character can be set in some variable in the sys module, but I don't know if it works for reading files, I assumed it was only for output.

Dusty

Offline

#3 2006-02-24 16:45:32

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python equivalent to Perl's "$/"?

Dusty wrote:

er.... I'd just read the whole line and use string.split() (think that's the syntax) to split it into a list and process on that. If that's what you're asking. I think the delimiter character can be set in some variable in the sys module, but I don't know if it works for reading files, I assumed it was only for output.

Dusty

Yeah, I know of various work arounds, involving the likes of split. What I'm investigating is for something more elegant. I don't want - if possible - to have to take care of remainders, especially if the record of interest spans over multiple newlines. So, read a line, split it, iterate over each element. What about the last element? is it complete? Do I need to carry it over to prepend to the next split, blah, blah, blah.

You may be on to something about the sys module. I have a feeling I've looked though.

Offline

#4 2006-02-24 17:10:02

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,771

Re: Python equivalent to Perl's "$/"?

readlines  :?


Mr Green

Offline

#5 2006-02-24 18:31:22

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Python equivalent to Perl's "$/"?

I don't understand your problem.... just read the entire file with one call (s = file.read()), then split it on whatever input, then forin the resulting list. Or is it that split only splits on single characters, not arbitrary length strings. I don't recall...

Dusty

Offline

#6 2006-02-24 19:32:57

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python equivalent to Perl's "$/"?

Dusty wrote:

I don't understand your problem.... just read the entire file with one call (s = file.read()), then split it on whatever input, then forin the resulting list. Or is it that split only splits on single characters, not arbitrary length strings. I don't recall...

Dusty

Oh yeah, I guess I forgot the other contraint, and that is that I'm working on very big files. I know I can read the entire file into memory, but that just makes my machine keel over! (or almost).

Not to worry, looks like I'll remain with the cumbersome way of accumulating lines until I reach my desired "delimiter". I just never thought I'd hear the day when I'd say "I wish Python did it the Perl way"!!!!

Offline

#7 2006-02-24 20:47:00

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,771

Re: Python equivalent to Perl's "$/"?

I wish Python did it the Perl way

Then do it in Ruby ;-)


Mr Green

Offline

#8 2006-02-24 21:54:24

tranquility
Member
From: Portugal
Registered: 2004-08-06
Posts: 136

Re: Python equivalent to Perl's "$/"?

arooaroo wrote:
Dusty wrote:

I don't understand your problem.... just read the entire file with one call (s = file.read()), then split it on whatever input, then forin the resulting list. Or is it that split only splits on single characters, not arbitrary length strings. I don't recall...

Dusty

Oh yeah, I guess I forgot the other contraint, and that is that I'm working on very big files. I know I can read the entire file into memory, but that just makes my machine keel over! (or almost).

Not to worry, looks like I'll remain with the cumbersome way of accumulating lines until I reach my desired "delimiter". I just never thought I'd hear the day when I'd say "I wish Python did it the Perl way"!!!!

Try searching for a thing called python generators. I've never used them myself, but I've come across a page when researching on how to implement lengthy operations with gtk. These generators might be worth your research.

Hope it helps.

Offline

#9 2006-03-01 14:40:28

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python equivalent to Perl's "$/"?

Mr Green wrote:

I wish Python did it the Perl way

Then do it in Ruby ;-)

So how would this problem be solved in Ruby?

Offline

#10 2006-03-01 16:46:17

joe
Member
From: Folsom, CA
Registered: 2004-07-27
Posts: 51
Website

Re: Python equivalent to Perl's "$/"?

Ruby uses the same input and output separator variables as Perl.

http://www.rubyist.net/~slagell/ruby/globalvars.html

Offline

#11 2006-03-01 21:48:46

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Python equivalent to Perl's "$/"?

Ooh. Thanks for pointing that out. Perhaps it is time...

Offline

Board footer

Powered by FluxBB