Re: [Tutor] Splitting
Abhinav Gaurav wrote: Example :- a=43;dsds;d b=a.split(';') Can anybody help on resolving this issue? This is what it looks like on my computer: a=43;dsds;d b=a.split(;) print b ['43', 'dsds', 'd'] This is using Python 2.4.2, though AFAI can recall this behaviour hasn't changed in a while... Weird... Sorry I couldn't be more help, Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Splitting strings into blocks
Hi list, I'm currently working on a program to parse LaTeX style maths expressions and provide an answer. For example, I have the expression 2^\frac{1}{2}. I'm trying to work out a way to split this into it's most basic blocks of LaTeX (i.e. 2^ and \frac{1}{2}) while maintaining a record of the depth of the expression (i.e. (2^,0),(\frac{1}{2},1)). I will then process this list from the highest order downwards, feeding the deeper results progressively into shallower elements until all have been calculated. LaTeX allows me to legally express the previously stated expression as {2^{\\frac{1}{2}}}. This makes it much easier to figure out where the units of LaTeX are located. The depth of any item can now be expressed as the number of unpaired opening or closing braces between the element and the start or end of the expression. I'm essentially looking for a way to split the string up along the braces, while recording the number of braces between the split and either end of the expression. Any help would be much appreciated. Cheers, Dan pgpLJN6CxlI0r.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Storing Dictionaries Externally
Currently, I'm writing a little project which needs to store a dictionary in an external file (so it can be accessed by another program). However, no matter how much I try, I cannot get Python to import the dictionary from the file properly. However, I know the problem. Whatever format I put the data in within the file (either as a dictionary or a list of tuples to 'dict()') Python reads it as a string, rather than a dictionary or list of tuples. My question is essentially this: How do I convert a string into a dictionary or list of tuples, assuming it's syntax is correct (ie. it is actually how a dictionary or list of tuples would look if I were defining it). Thanks in advance, Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] FW: Help Needed
When I am in Python Shell of the IDLE GUI, when I click Edit/Run Script, I am getting a dialog box that is titled “Not saved” and states “The buffer for Python shell is not saved. Please save it first.” With an “OK” button that just takes me back to the Shell window. I am going through a book (which suggests this resource as a good place to ask questions) and Edit/Run Script was working just fine the other day. Go to File Save As... and save the file somewhere. Then attempt to run the script. If this still doesn't work, I'm stumped, but it really should... Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python strings?
On Thu, 2005-09-08 at 15:15 +0100, [EMAIL PROTECTED] wrote: for i in range(10): principal = principal * (1 + apr) To calculate compound interest, you in fact don't need to use a loop at all (you could use: 'final = principal * (apr ** years)') but if you really want to use a loop, I would use a while loop (although this may not be the best way involving loops, my loop-fu isn't great). The while loop would look like: while years: principal = principal * (1 + apr) years = years - 1 If you planned to use fractions of years, then you need to tweak this. print The value in years is, principal To actually answer your question, in order to print this correctly, you need to tell Python to convert the integer (principal) to a string by using the built-in function str() like so: print The value in years is, str(principal) To include the years, you would just add 'str(years)' in an appropriate position, although if you've used the while loop, then referring to years will simply give you 0, as you've been adjusting it. The solution to this is to assign 'years' to a different variable initially (and use one in the loop and the other here) in order to preserve it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python strings?
On Thu, 2005-09-08 at 21:00 +0100, Alan G wrote: Not so. print automatically calls str() on all its arguments so the programmer doesn't need to. So it does. I think I was trying to concatenate, which it doesn't like. Sorry if I caused any confusion. Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Print Output Location in Windows XP
On Sat, 2005-09-03 at 03:30 +0100, Tom Strickland wrote: How do I get the output window to stay open, or where is the output stored? You can open up a command prompt before running the program, which will allow you to see the output as the program runs. You get to this by 'Start Run..' or something similar. As you're using XP, you then type in 'cmd' which should bring up a command line. Navigate your way to the appropriate place and run the program. Alternatively, you could dump the output into a text file, but I have no idea how to do that in Windows so someone else'll have to pick that up for you. Hope that helps, Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Remote Directory Reading
I've run into a bit of trouble with my spider script. Thus far, it is able to retrieve all of the data off the website that is contained within standard HTML, downloading jpg, gif and bmp images that are related to the files (this restriction only being set by a lack of further definitions by myself). However, I have run into a problem with one link that simply points to a folder (www.aehof.org.uk/forum) within which is contained a phpBB forum. I've attempted to use 'dircache' but couldn't find a way for it to understand web addresses. However, I may not have hit upon the right combination of syntax, so may be mistaken. I also considered 'os' but it appears to require definition of a particular operating system, which is a direction I'd prefer not to take unless I have to. In addition, the error messages I received from using 'dircache' traced back into 'os' so it is unlikely it would have been suitable for the purpose. The variables at the top of the script will later be cleaned up so they are defined by command line input, they're just there while I sort the actual script itself out. Succinctly, I'm looking for an addition to the following script (ideally under 'default_defs') which will allow me to copy a directly-linked folder and all its contents. I hope I have expressed myself in a useful manner, and any help would be appreciated, although I would prefer no comments on the script in general as I would quite like to develop it myself as a learning project (this being my first proper script). Cheers muchly, Dan spider.py import re import urllib import distutils.dir_util site = http://www.aehof.org.uk; localdir = /home/daniel/test/ default_defs='[cf]=.*html|c=.*jpg|c=.*gif|c=.*bmp' if not re.search('/\Z',site): site = site + / def get_page_items (source, site = site, defs = default_defs): next = [] text = urllib.urlopen(site+source).read() if re.search(defs,text): for i in re.findall(defs,text): i = i[3:-1] next.append(i) src = [source] + next return src def get_list (source=index.html): items = [] next = get_page_items (source) for i in next: if i not in items: items.append (i) next.extend (get_page_items (i)) return items def copy_items (): items = get_list() for i in items: distutils.dir_util.create_tree(localdir, items) original = urllib.urlopen(site+i) local = open(localdir+i,'w') body = original.read() local.write(body) local.close() copy_items() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Website Retrieval Program
I'm currently trying to write a script that will get all the files necessary for a webpage to display correctly, followed by all the intra-site pages and such forth, in order to try and retrieve one of the many sites I have got jumbled up on my webspace. After starting the writing, someone introduced me to wget, but I'm continuing this because it seems like fun (and that statement is the first step on a slippery slope :P). My script thus far reads: import re import urllib source = urllib.urlopen(http://www.aehof.org.uk/real_index2.html;) next = re.findall('src=.*html',source.read()) print next This returns the following: ['src=nothing_left.html', 'src=testindex.html', 'src=nothing_right.html'] This is a good start (and it took me long enough! :P), but, ideally, the re would strip out the 'src=' as well. Does anybody with more re-fu than me know how I could do that? Incidentally, feel free to use that page as an example. In addition, I am aware that this will need to be adjusted and expanded later on, but it's a start. Thanks in advance, Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor