[Tutor] big numbers
Dear all, I'm new to Python, and using Sam's Teach Yourself Python in 24 Hours. The book introduces big numbers, saying that I should expect the following problem: >>> 1 + 99 OverflowError: integer literal too large The "problem" is that this doesn't happen. When I execute this command, Python seems to automatically convert the result into a big number: >>> 1 + 99 100L This book is a few years old and was written for Python version 1.5, and of course I'm using version 2.3, so I'm just wondering if this whole issue of big numbers is now being handled automatically? This is my first post - sorry to be asking such a basic question. TIA, Robert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do you use pydoc?
[EMAIL PROTECTED] said unto the world upon 2005-03-14 19:28: I have read but don't under stand how to use pydoc. here what i read can't figer out how to use it. Hi, try this: Fire up IDLE and your web browser of choice. In IDLE's shell, type: import HTMLParser There is nothing special about the HTMLParser module other than that it is a pure Python module -- I just wanted to pick a concrete case for the discussion here. Now type: help(HTMLParser) There. You are now using pydoc :-) It is displaying the documentation for the module HTMLParser. Now try typing help(HTMLParser.HTMLParser) That's the documentation for one class in the module. help(HTMLParser.HTMLParser.close) The documentation for one method of that class. Now, in the Start Menu entries for Python, find the icon for Module Docs. Click it, and then click open browser. Scroll down until you see HTMLParser, and click on it. Your web browser should give you the same info as you got from help(HTMLParser), but in a nicer format. That's pydoc at work, too. In IDLE's editor, open HTMLParser.py (mine's at C:\Python24\Lib\HTMLParser.py). Now, look at the file HTMLParser.py and compare that to the outputs that you got above. See if you can see where the help() command got its output from. The parts of the output that come from text in triple quotes are docstrings. Put them in your own modules and functions, and you can use help or Module Docs to remind you of what you own code does. Save a backup copy of HTMLParser.py, and try adding docstrings to methods like HTMLParser.HTMLParser.handle_comment, and see if you can see how and why the '#' lines ended up in the help() and Module Docs output. (Don't forget to save your changes before trying to view them in your web browser with Module Docs -- and don't forget to restore the backup when you are done! This is easier to do with Module Docs than in the IDLE shell as in the shell pydoc won't know about your changes unless you reload the module.) Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
On Mar 15, 2005, at 03:35, jrlen balane wrote: how am i going to change the filename automaticaly? for example: #every 5 minutes, i am going to create a file based on the data above for i in range(100) output_file = file('c:/output' +.join(i) +'.txt', 'w') #guess this won't work output_file.writelines(lines) output_file.close() I'm not going to give you a complete solution because that'd spoil the fun, but you need to use the os.path.exists function (in the os module), which tells you if a file exists or not (no need to open it -- actually, your loop is dangerous: opening a file with "w" as the mode erases it). As for generating the file name, you've almost got it. Just remove that bizarre call to "join", cause that won't work, and replace your for loop with a while loop, so that you can break out of it when you've found the correct file name, not before or after. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
Whoops, golden rule - "Never post untested code" Sorry. On Mon, 14 Mar 2005 21:05:44 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > jrlen balane wrote: > > ok, i've done what sir Kent just said, my fault... > > > > but an error still occurs: > > Traceback (most recent call last): > > File "C:\Python23\practices\opentxtprintlngnew.py", line 18, in -toplevel- > > print process(data) > > File "C:\Python23\practices\opentxtprintlngnew.py", line 10, in process > > tempLine = int(line) > > ValueError: invalid literal for int(): abc > > > > isn't this the job of : > > > > except TypeError: > > print "Non numeric character in line", line > > continue #Breaks, and starts with next line > > Yes, only it should be ValueError instead of TypeError. You can check this > interactively: > >>> int('foo') > Traceback (most recent call last): >File "", line 1, in ? > ValueError: invalid literal for int(): foo > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a tab delimited filename
so for example, i have 5 arrays, i can do this (is this correct): data1[1,2,3,4,5 (and so on)] data2[1,2,3,4,5 (and so on)] data3[1,2,3,4,5 (and so on)] data4[1,2,3,4,5 (and so on)] data5[1,2,3,4,5 (and so on)] datas = [data1, data2, data3, data4, data5] for data in datas: lines.append('\t'.join(data) + '\n') = (supposed to be output) 1 234 5 ... 1 234 5 ... 1 234 5 ... ... (but i want this output) 111 11 222 22 333 33 ... ... ... ...... = output_file = file('c:/output.txt', 'w') output_file.writelines(lines) output_file.close() = how am i going to change the filename automaticaly? for example: #every 5 minutes, i am going to create a file based on the data above for i in range(100) output_file = file('c:/output' +.join(i) +'.txt', 'w') #guess this won't work output_file.writelines(lines) output_file.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
jrlen balane wrote: ok, i've done what sir Kent just said, my fault... but an error still occurs: Traceback (most recent call last): File "C:\Python23\practices\opentxtprintlngnew.py", line 18, in -toplevel- print process(data) File "C:\Python23\practices\opentxtprintlngnew.py", line 10, in process tempLine = int(line) ValueError: invalid literal for int(): abc isn't this the job of : except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line Yes, only it should be ValueError instead of TypeError. You can check this interactively: >>> int('foo') Traceback (most recent call last): File "", line 1, in ? ValueError: invalid literal for int(): foo Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
ok, i've done what sir Kent just said, my fault... but an error still occurs: Traceback (most recent call last): File "C:\Python23\practices\opentxtprintlngnew.py", line 18, in -toplevel- print process(data) File "C:\Python23\practices\opentxtprintlngnew.py", line 10, in process tempLine = int(line) ValueError: invalid literal for int(): abc isn't this the job of : except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line On Mon, 14 Mar 2005 20:22:29 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > jrlen balane wrote: > > import sys > > > > data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') > > data = data_file.readlines() > > > > def process(list_of_lines): > > data_points = [] > > for line in list_of_lines: > > try: > > tempLine = int(line) > > except TypeError: > > print "Non numeric character in line", line > > continue #Breaks, and starts with next line > > > > data_points.append(tempLine) > > return data_points > > > > print process(data) > > > > == > > > > [1000] > > > > == > > same result :( any other suggestion??? > > It doesn't look to me like you changed the 'return datapoints' line at all? > Indentation is > significant in Python; by indenting the 'return' past the 'for', you make the > return part of the > loop. The effect of this is to break out of the loop after the first line, > which is what you are seeing. > > Try this: > > import sys > > data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') > data = data_file.readlines() > > def process(list_of_lines): > data_points = [] > for line in list_of_lines: > try: >tempLine = int(line) > except TypeError: >print "Non numeric character in line", line >continue #Breaks, and starts with next line > > data_points.append(tempLine) > return data_points This line was moved left four spaces > now it is not part of the loop > > print process(data) > > Kent > > > > On Mon, 14 Mar 2005 19:57:26 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > > > >>jrlen balane wrote: > >> > >>>this is what i get after running this on IDLE: > >>> > >>>import sys > >>> > >>>data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') > >>>data = data_file.readlines() > >>> > >>>def process(list_of_lines): > >>>data_points = [] > >>>for line in list_of_lines: > >>>try: > >>> tempLine = int(line) > >>>except TypeError: > >>> print "Non numeric character in line", line > >>> continue #Breaks, and starts with next line > >>> > >>>data_points.append(tempLine) > >>>return data_points > >> > >>This line ^^^ is indented four spaces too much - you are returning after > >>the first time through the > >>loop. Indent it the same as the for statement and it will work correctly. > >> > >>Kent > >> > >>>print process(data) > >>> > >>>= > >>>[1000] > >>> > >>>== > >>>but this is what i have written on the text file: > >>> > >>>1000 > >>>890 > >>>900 > >> > >>___ > >>Tutor maillist - Tutor@python.org > >>http://mail.python.org/mailman/listinfo/tutor > >> > > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] what is the "path browser" used for?
On Mar 15, 2005, at 00:55, [EMAIL PROTECTED] wrote: What is the path browser for and all the sys.path's for? are these for copying and pasteing to help you on your program? No, sys.path (as explained by pydoc sys ;) ) is the module search path; that is, the list of the folders into which Python looks for a module when you try to import it. Oh, and on an unrelated topic, could you please turn off HTML in your e-mails? Blue text on green background is not very comfortable to read... Thanks! -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
jrlen balane wrote: import sys data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') data = data_file.readlines() def process(list_of_lines): data_points = [] for line in list_of_lines: try: tempLine = int(line) except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line data_points.append(tempLine) return data_points print process(data) == [1000] == same result :( any other suggestion??? It doesn't look to me like you changed the 'return datapoints' line at all? Indentation is significant in Python; by indenting the 'return' past the 'for', you make the return part of the loop. The effect of this is to break out of the loop after the first line, which is what you are seeing. Try this: import sys data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') data = data_file.readlines() def process(list_of_lines): data_points = [] for line in list_of_lines: try: tempLine = int(line) except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line data_points.append(tempLine) return data_points This line was moved left four spaces now it is not part of the loop print process(data) Kent On Mon, 14 Mar 2005 19:57:26 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: jrlen balane wrote: this is what i get after running this on IDLE: import sys data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') data = data_file.readlines() def process(list_of_lines): data_points = [] for line in list_of_lines: try: tempLine = int(line) except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line data_points.append(tempLine) return data_points This line ^^^ is indented four spaces too much - you are returning after the first time through the loop. Indent it the same as the for statement and it will work correctly. Kent print process(data) = [1000] == but this is what i have written on the text file: 1000 890 900 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
import sys data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') data = data_file.readlines() def process(list_of_lines): data_points = [] for line in list_of_lines: try: tempLine = int(line) except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line data_points.append(tempLine) return data_points print process(data) == >>> [1000] >>> == same result :( any other suggestion??? On Mon, 14 Mar 2005 19:57:26 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > jrlen balane wrote: > > this is what i get after running this on IDLE: > > > > import sys > > > > data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') > > data = data_file.readlines() > > > > def process(list_of_lines): > > data_points = [] > > for line in list_of_lines: > > try: > > tempLine = int(line) > > except TypeError: > > print "Non numeric character in line", line > > continue #Breaks, and starts with next line > > > > data_points.append(tempLine) > > return data_points > This line ^^^ is indented four spaces too much - you are returning after the > first time through the > loop. Indent it the same as the for statement and it will work correctly. > > Kent > > > > print process(data) > > > > = > > [1000] > > > > == > > but this is what i have written on the text file: > > > > 1000 > > 890 > > 900 > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
jrlen balane wrote: this is what i get after running this on IDLE: import sys data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') data = data_file.readlines() def process(list_of_lines): data_points = [] for line in list_of_lines: try: tempLine = int(line) except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line data_points.append(tempLine) return data_points This line ^^^ is indented four spaces too much - you are returning after the first time through the loop. Indent it the same as the for statement and it will work correctly. Kent print process(data) = [1000] == but this is what i have written on the text file: 1000 890 900 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] what is the "path browser" used for?
What is the path browser for and all the sys.path's for? are these for copying and pasteing to help you on your program? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do you use pydoc?
On Mar 15, 2005, at 00:28, [EMAIL PROTECTED] wrote: I have read but don't under stand how to use pydoc. here what i read can't figer out how to use it. pydoc is more or less a help browser. Think of it as "man for Python". If you need documentation on a module, just type "pydoc [module name]" at a command prompt. For example, if you want to know how the os module works, just type "pydoc os". Even better: if the modules you're writing use docstrings, then calling pydoc on your module will work automagically. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
this is what i get after running this on IDLE: import sys data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') data = data_file.readlines() def process(list_of_lines): data_points = [] for line in list_of_lines: try: tempLine = int(line) except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line data_points.append(tempLine) return data_points print process(data) = [1000] == but this is what i have written on the text file: 1000 890 900 abc 500 650 850 1200 1100 On Tue, 15 Mar 2005 12:53:26 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote: > Oops, and I meant > > try: >tempLine = int(line) > > Silly indent error. > > > On Tue, 15 Mar 2005 12:52:49 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote: > > Well, a string "12345" when called through int() will come back as 12345. > > > > But, a string "foo", called through int(), will raise a TypeError. > > > > So > > > > > import sys > > > > > > data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') > > > data = data_file.readlines() > > > > > > def process(list_of_lines): > > > data_points = [] > > > for line in list_of_lines: > > > data_points.append(int(line)) > > > return data_points > > > > > > print process(data) > > > > You could do this > > > > def process(list_of_lines): > > data_points=[] > > for line in list_of_lines: > >try: > >tempLine = int(line) > >except TypeError: > >print "Non numeric character in line", line > >continue #Breaks, and starts with next line > >data_points.append(tempLine) > > > > That's one way, but there's probably a better way. > > > > Regards, > > > > Liam Clarke > > -- > > 'There is only one basic human right, and that is to do as you damn well > > please. > > And with it comes the only basic human duty, to take the consequences. > > > > -- > 'There is only one basic human right, and that is to do as you damn well > please. > And with it comes the only basic human duty, to take the consequences. > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How do you use pydoc?
I have read but don't under stand how to use pydoc. here what i read can't figer out how to use it. 5.1 pydoc -- Documentation generator and online help system New in version 2.1. The pydoc module automatically generates documentation from Python modules. The documentation can be presented as pages of text on the console, served to a Web browser, or saved to HTML files. The built-in function help() invokes the online help system in the interactive interpreter, which uses pydoc to generate its documentation as text on the console. The same text documentation can also be viewed from outside the Python interpreter by running pydoc as a script at the operating system's command prompt. For example, running pydoc sys at a shell prompt will display documentation on the sys module, in a style similar to the manual pages shown by the Unix man command. The argument to pydoc can be the name of a function, module, or package, or a dotted reference to a class, method, or function within a module or module in a package. If the argument to pydoc looks like a path (that is, it contains the path separator for your operating system, such as a slash in Unix), and refers to an existing Python source file, then documentation is produced for that file. Specifying a -w flag before the argument will cause HTML documentation to be written out to a file in the current directory, instead of displaying text on the console. Specifying a -k flag before the argument will search the synopsis lines of all available modules for the keyword given as the argument, again in a manner similar to the Unix man command. The synopsis line of a module is the first line of its documentation string. You can also use pydoc to start an HTTP server on the local machine that will serve documentation to visiting Web browsers. pydoc -p 1234 will start a HTTP server on port 1234, allowing you to browse the documentation at http://localhost:1234/ in your preferred Web browser. pydoc -g will start the server and additionally bring up a small Tkinter-based graphical interface to help you search for documentation pages. When pydoc generates documentation, it uses the current environment and path to locate modules. Thus, invoking pydoc spam documents precisely the version of the module you would get if you started the Python interpreter and typed "import spam". Module docs for core modules are assumed to reside in http://www.python.org/doc/current/lib/. This can be overridden by setting the PYTHONDOCS environment variable to a different URL or to a local directory containing the Library Reference Manual pages. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
Oops, and I meant try: tempLine = int(line) Silly indent error. On Tue, 15 Mar 2005 12:52:49 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote: > Well, a string "12345" when called through int() will come back as 12345. > > But, a string "foo", called through int(), will raise a TypeError. > > So > > > import sys > > > > data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') > > data = data_file.readlines() > > > > def process(list_of_lines): > > data_points = [] > > for line in list_of_lines: > > data_points.append(int(line)) > > return data_points > > > > print process(data) > > You could do this > > def process(list_of_lines): > data_points=[] > for line in list_of_lines: >try: >tempLine = int(line) >except TypeError: >print "Non numeric character in line", line >continue #Breaks, and starts with next line >data_points.append(tempLine) > > That's one way, but there's probably a better way. > > Regards, > > Liam Clarke > -- > 'There is only one basic human right, and that is to do as you damn well > please. > And with it comes the only basic human duty, to take the consequences. > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
Well, a string "12345" when called through int() will come back as 12345. But, a string "foo", called through int(), will raise a TypeError. So > import sys > > data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') > data = data_file.readlines() > > def process(list_of_lines): > data_points = [] > for line in list_of_lines: > data_points.append(int(line)) > return data_points > > print process(data) You could do this def process(list_of_lines): data_points=[] for line in list_of_lines: try: tempLine = int(line) except TypeError: print "Non numeric character in line", line continue #Breaks, and starts with next line data_points.append(tempLine) That's one way, but there's probably a better way. Regards, Liam Clarke -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read from a txt file
say i have the code that reads decimal value from a text file: import sys data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r') data = data_file.readlines() def process(list_of_lines): data_points = [] for line in list_of_lines: data_points.append(int(line)) return data_points print process(data) what if, on the text file, a user has encoded values other than decimal, how would i add a code that would act like an Exception, or would tell the user that there is an invalid entry in the text file, like a "letter or other character other than number" ? On Thu, 17 Feb 2005 01:44:19 -0800 (PST), Danny Yoo <[EMAIL PROTECTED]> wrote: > > > > >> Traceback (most recent call last): > > >> File "C:\Python23\practices\opentxt", line 12, in -toplevel- > > >> process(data) > > >> File "C:\Python23\practices\opentxt", line 6, in process > > >> data_points.append(int(line)) > > >> ValueError: invalid literal for int(): > > Hi Brian, > > Ah, think about empty lines. > > Let's look at the error message again: > > ValueError: invalid literal for int(): > ^^^ > > There's nothing visible there after the colon, and that's our hint. > Notice what happens when we pass int() some wacky strings: > > ### > >>> int("foobar") > Traceback (most recent call last): > File "", line 1, in ? > ValueError: invalid literal for int(): foobar > ### > > So whatever is being passed to int() should show up in the error message. > This is exactly why getting literal error messages is so wonderful. > *grin* > > Since we don't see anything here: > > > >> File "C:\Python23\practices\opentxt", line 12, in -toplevel- > > >> process(data) > > >> File "C:\Python23\practices\opentxt", line 6, in process > > >> data_points.append(int(line)) > > >> ValueError: invalid literal for int(): > > my best guess is that there's an empty line in the file, since we get the > same kind of error if we do this: > > ### > >>> int("") > Traceback (most recent call last): > File "", line 1, in ? > ValueError: invalid literal for int(): > ### > > Best of wishes to you! > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CGI authentication
On Mon, Mar 14, 2005 at 08:04:10AM -0500, Kent Johnson wrote: > Date: Mon, 14 Mar 2005 08:04:10 -0500 > From: Kent Johnson <[EMAIL PROTECTED]> > User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) > To: Python Tutor > Subject: [Tutor] CGI authentication > > There was a question on this list recently about how to authenticate users > of a web server from a simple CGI script. I just came across a module that > might help: > http://www.voidspace.org.uk/python/logintools.html > > Kent > That would be for me! This looks pretty nice. On problem I have with the python url2lib is the dfficulty of determining the realm. For example, bot lynx and curl (command line url tools) don't need a realm to work. This seems to mean that lynx and curl provide more flexibility. Paul -- *Paul Tremblay * [EMAIL PROTECTED]* ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re help
And I wouldn't mind mentioning that re is slightly over kill for the examples given. Try this. ### old_group_delimiter = "\n\n" old_line_delimiter = "\n" new_group_delimiter = "\n" new_line_delimiter = ", " fi = raw_input("Input file to use? ") fi = open(fi,"r") stuff = fi.read() fi.close() addressgroups = stuff.split(old_group_delimiter) addressgroups = [x.split(old_line_delimiter) for x in addressgroups] addressgroups = [new_line_delimiter.join(x) for x in addressgroups] newtext = new_group_delimiter.join(addressgroups) fi = raw_input("Output file to use? ") fi = open(fi,"w") fi.write(newtext) fi.close() ### But your probably using re to study it, though. Just an alternative option. I'm trying to keep all the different ways in my head you know... Jacob S. The following program takes text data like this: Jimi Hendrix 2100 South Ave Seattle, WA 55408 and changes it to this Jimi Hendrix, 2100 South Ave,Seattle,WA,55488 and writes it to a file. The problem I'm running into is that it only writes this first address to a file and there are several others in the file. I believe it has something to do with using re.search instead of re.findall. But re.findall returns a error when I try using it. Suggestions? Thanks in advance. Here is the script: import re f = open('reformat.txt').read() pat = re.compile(r"([^\r\n]+)\n([^\r\n]*)\n([^\r\n]*) ([^\r\n]*) ([^\r\n]*)") x=re.search(pat,f) name = x.group(1) address = x.group(2) citystate = x.group(3)+x.group(4) zipcd = x.group(5) o= open('reformat1.txt','w') o.write("%s,%s,%s,%s\n" % (name, address, citystate,zipcd)) o.close() print("%s,%s,%s,%s\n" % (name, address, citystate,zipcd)) __ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python>data>sqlite>python>data>paper
For example a balance report for a company. Regards Jan - Original Message - From: "Danny Yoo" <[EMAIL PROTECTED]> To: "Jan EkstrÃm" <[EMAIL PROTECTED]> Cc: Sent: Monday, March 14, 2005 3:14 AM Subject: Re: [Tutor] python>data>sqlite>python>data>paper On Sat, 12 Mar 2005, [iso-8859-1] Jan Ekstrm wrote: I have looked through a lot of tutor documentation. But I would ask someone to show me python code for putting som data in a sqlite database and code showing how I get data out of such a database ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] CGI authentication
There was a question on this list recently about how to authenticate users of a web server from a simple CGI script. I just came across a module that might help: http://www.voidspace.org.uk/python/logintools.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re help
Ron Nixon wrote: The following program takes text data like this: Jimi Hendrix 2100 South Ave Seattle, WA 55408 and changes it to this Jimi Hendrix, 2100 South Ave,Seattle,WA,55488 and writes it to a file. Hameed has shown you one solution. I would like to point out that if you plan to read this data back in to a program, the format you have chosen is problematic. You can't count on the number of commas being fixed. For example, the address John Doe, Sr. 2100 South Ave Seattle WA 55408 would become John Doe, Sr.,2100 South Ave,Seattle WA,55408 If you try to split this at the commas you will not get the correct result. One solution is to use the csv module which will quote the strings containing commas. Kent The problem I'm running into is that it only writes this first address to a file and there are several others in the file. I believe it has something to do with using re.search instead of re.findall. But re.findall returns a error when I try using it. Suggestions? Thanks in advance. Here is the script: import re f = open('reformat.txt').read() pat = re.compile(r"([^\r\n]+)\n([^\r\n]*)\n([^\r\n]*) ([^\r\n]*) ([^\r\n]*)") x=re.search(pat,f) name = x.group(1) address = x.group(2) citystate = x.group(3)+x.group(4) zipcd = x.group(5) o= open('reformat1.txt','w') o.write("%s,%s,%s,%s\n" % (name, address, citystate,zipcd)) o.close() print("%s,%s,%s,%s\n" % (name, address, citystate,zipcd)) __ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] funny behaviour
Jacob Abraham wrote: Dear Tutors, A class was created to extend timedelta to add and subtract months. Simple doctests that simply create an instance of the class failed. Could someone please explain this really funny behaviour. timedelta is an immutable class (its instances have fixed values that can't be changed). When you subclass an immutable class you have to override __new__ instead of __init__. See this link for details and examples: http://www.python.org/2.2.3/descrintro.html#__new__ Kent Regards, Jacob Abraham from datetime import datetime, timedelta class WeirdTimeDelta(timedelta): """Allows addition and subtraction of months. Variables are getting passed to the timedelta constructor ?? >>> delta = WeirdTimeDelta(5) >>> delta.days 0 Should'nt this work ??? >>> delta = WeirdTimeDelta(months=5) """ def __init__(self, months=0, *vals, **kwds): """Constructs a weird time delta.""" super(WeirdTimeDelta, self).__init__(*vals, **kwds) self.months = months if __name__ == "__main__": import doctest doctest.testmod() __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re help
Hi, Ron! I am also a newbie in programming. But after reading your problem i decided to solve it as a homework :). But there are few things you didn't mentioned. Does all the addresses in first file have same format. What seperates those addresses in the file. Assuming that all address are on 3 lines seperated by a blank line (The address file should have a blank line after the last address otherwise it will not read the last address). i have made the following program. #!/usr/bin/python # format.py import re f = open("reformat.txt").read() pat = re.compile(r"((?:.*\n)+?\n)",re.M) res = pat.findall(f) out = open("reformat1.txt","w") for addy in res: addy = ", ".join(addy[:-2].split("\n")) out.write(addy + "\n") out.close() # End This program works fine on my Linux box. And hopefully it will work too on Windows. I would also ask other experienced programmers that is there any other better way we can do it :). Happy Programming. On Sun, 13 Mar 2005 21:26:08 -0800 (PST), Ron Nixon <[EMAIL PROTECTED]> wrote: > The following program takes text data like this: > Jimi Hendrix > 2100 South Ave > Seattle, WA 55408 > > and changes it to this > > Jimi Hendrix, 2100 South Ave,Seattle,WA,55488 > > and writes it to a file. The problem I'm running into > is that it only writes this first address to a file > and there are several others in the file. I believe it > has something to do with using re.search instead of > re.findall. But re.findall returns a error when I try > using it. Suggestions? Thanks in advance. > Here is the script: > > import re > f = open('reformat.txt').read() > pat = re.compile(r"([^\r\n]+)\n([^\r\n]*)\n([^\r\n]*) > ([^\r\n]*) ([^\r\n]*)") > x=re.search(pat,f) > name = x.group(1) > address = x.group(2) > citystate = x.group(3)+x.group(4) > zipcd = x.group(5) > o= open('reformat1.txt','w') > o.write("%s,%s,%s,%s\n" % (name, address, > citystate,zipcd)) > o.close() > print("%s,%s,%s,%s\n" % (name, address, citystate,zipcd)) > > > __ > Do you Yahoo!? > Yahoo! Small Business - Try our new resources site! > http://smallbusiness.yahoo.com/resources/ > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Regards, Hameed U. Khan Registered Linux User #: 354374 - *Computer without Linux is just like the world without computer.* ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor