[Tutor] newbie: Reading text file

Grant Hagstrom grantahagstrom at gmail.com
Fri Jun 1 10:12:13 CEST 2007


Hey,

I'm a newbie and this is my first script submission to this email list.
I was able to parse out the jobs list into a string: "jobs = [ . . ."
However, I can't make python interpret that string as the command "jobs = [
some list]"

#SCRIPT

# open the file and assign it to the variable "thefile"
thefile = open("/home/banter/Desktop/mylist.py")

# read the file and assign it to the variable "read_thefile"
read_thefile = file.read(thefile)

# make the file's contents into a string
string_thefile = str(read_thefile)

# split that string up into a list
splitlist_thefile = string_thefile.split()

# index the list to find the placement of the word 'jobs'
it_starts = splitlist_thefile.index('jobs')

# index the list to find the placement of the ']'
# bug warning: an item in list "job" with a bracket will
#    throw off this script
# Also, there needs to be a comparison between instances of
# ']' such that the returned index number will be the smallest
# number that is still larger than 'it_starts'
it_almost_ends = splitlist_thefile.index(']')

# the ending index number is not good enough
# we need to add a 1 to the number
# read this to find out why
# http://www.diveintopython.org/native_data_types/lists.html
it_ends = it_almost_ends + 1

# assign the data that you want to a variable
# if you have a question about this, then you
# probably didn't read the link above
wanted_data = splitlist_thefile[it_starts:it_ends]

# join it all up
joined = " ".join(wanted_data)

print joined

#END SCRIPT

So close yet so far away!

oh, and Alan, are you getting a duplicate copy of this email? I am CCing
tutor at python and TOing you . . .

Best,

Grant Hagstrom

On 6/1/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
> "Preecha Bundrikwong" <preecha88 at gmail.com> wrote
>
> > I have a text file (mylist.py actually), it contains exactly below:
> > ---------------
> > # file mylist.py
> > jobs = [
> >        'Lions',
> >        'SysTest',
> >        ]
> > ----------------
> >
> > I want to write another script and get the list "jobs" from the
> > above
> > script.
>
> Because this is a python file and python files are effectively
> modules you can use import:
>
> import mylist
> print mylist.jobs
>
> > Is it possible for me to get the variable 'jobs' and its value from
> > the file
> > (as a list)? What I'm trying to do now is open the file, readlines
> > in a
> > loop, once it finds the line with "jobs = [", then start accumulate
> > (append)
> > a varible until it hits the line with "]" then stop.
>
> If it wasn't a python file (some other language say) then that is
> exactly what you would need to do. Programming often makes
> apparently simple things seem complicated! :-)
>
> Alan G.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070601/6733241c/attachment.htm 


More information about the Tutor mailing list