[Tutor] is it possible to embed data inside a script?

Lloyd Kvam pythontutor@venix.com
Sat Jul 19 10:58:02 2003


There is no read data ability in Python as there is in Basic and Perl.

You can certainly imbed the data into a python script and process it.
One approach:

DATA = '''abc 123
def 456
ghi 789'''
DATA_lines = DATA.split('\n')
DATA_items = [line.split() for line in DATA_lines]
for x,y in DATA_items:
	# do your thing here

If you prefer to keep things compact:
for x,y in [line.split() for line in DATA.split('\n')]:

Joseph Paish wrote:

> can a person include the data that a script processes inside the script 
> itself?  
> 
> i am in the process of converting a short perl script that processes 
> everything following __DATA__ instead of processing the contents of a 
> separate (very small) file.
> 
> 
> stripped down perl code follows :
> 
> # -------------
> my @lines = <DATA> ;
> 
> foreach (@lines) {
>     # do stripped out processing of stuff following __DATA__
>     }
> 
> __DATA__
> abc 123
> def 456
> ghi 789
> 
> # --------
> 
> 
> is there some way to achieve the same thing in python?
> 
> thanks
> 
> joe
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-443-6155
fax:	801-459-9582