Hey,<br><br>I'm a newbie and this is my first script submission to this email list.<br>I was able to parse out the jobs list into a string: "jobs = [ . . ."<br>However, I can't make python interpret that string as the command "jobs = [ some list]"
<br><br>#SCRIPT<br><br># open the file and assign it to the variable "thefile"<br>thefile = open("/home/banter/Desktop/mylist.py") <br><br># read the file and assign it to the variable "read_thefile"
<br>read_thefile = file.read(thefile)<br><br># make the file's contents into a string<br>string_thefile = str(read_thefile)<br><br># split that string up into a list<br>splitlist_thefile = string_thefile.split()<br><br>
# index the list to find the placement of the word 'jobs'<br>it_starts = splitlist_thefile.index('jobs')<br><br># index the list to find the placement of the ']'<br># bug warning: an item in list "job" with a bracket will
<br># throw off this script<br># Also, there needs to be a comparison between instances of<br># ']' such that the returned index number will be the smallest<br># number that is still larger than 'it_starts'
<br>it_almost_ends = splitlist_thefile.index(']')<br><br># the ending index number is not good enough<br># we need to add a 1 to the number<br># read this to find out why<br># <a href="http://www.diveintopython.org/native_data_types/lists.html">
http://www.diveintopython.org/native_data_types/lists.html</a><br>it_ends = it_almost_ends + 1<br><br># assign the data that you want to a variable<br># if you have a question about this, then you<br># probably didn't read the link above
<br>wanted_data = splitlist_thefile[it_starts:it_ends]<br><br># join it all up<br>joined = " ".join(wanted_data)<br><br>print joined<br><br>#END SCRIPT<br><br>So close yet so far away!<br><br>oh, and Alan, are you getting a duplicate copy of this email? I am CCing
tutor@python and TOing you . . .<br><br>Best,<br><br>Grant Hagstrom<br><br><div><span class="gmail_quote">On 6/1/07, <b class="gmail_sendername">Alan Gauld</b> <<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>"Preecha Bundrikwong" <<a href="mailto:preecha88@gmail.com">
preecha88@gmail.com</a>> wrote<br><br>> I have a text file (mylist.py actually), it contains exactly below:<br>> ---------------<br>> # file mylist.py<br>> jobs = [<br>> 'Lions',<br>> 'SysTest',
<br>> ]<br>> ----------------<br>><br>> I want to write another script and get the list "jobs" from the<br>> above<br>> script.<br><br>Because this is a python file and python files are effectively
<br>modules you can use import:<br><br>import mylist<br>print mylist.jobs<br><br>> Is it possible for me to get the variable 'jobs' and its value from<br>> the file<br>> (as a list)? What I'm trying to do now is open the file, readlines
<br>> in a<br>> loop, once it finds the line with "jobs = [", then start accumulate<br>> (append)<br>> a varible until it hits the line with "]" then stop.<br><br>If it wasn't a python file (some other language say) then that is
<br>exactly what you would need to do. Programming often makes<br>apparently simple things seem complicated! :-)<br><br>Alan G.<br><br><br>_______________________________________________<br>Tutor maillist - <a href="mailto:Tutor@python.org">
Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>