[Tutor] Should I use python for parsing text
I am using an intel iMac with OS -X 10.4.8. It has Python 2.3.5. My issue is that I have a lot of text ( about 500 pages at the moment) that I need to parse so that I can eliminate info I don't need, break the remainder into fields and put in a database/spreadsheet. See example next: A.-C. Manufacturing Company. (See Sebastian, A. A., and Capes, assignors.) A. G. A. Railway Light & Signal Co. (See Meden, Elof H„ assignor.) A-N Company, The. (See Alexander and Nasb, as- signors.; AN Company, The. (See Nash, It. J., and Alexander, as- signors.) A/S. Arendal Smelteverk.(See Kaaten, Einar, assignor.) A/S. Bjorgums Gevaei'kompani. (See Bjorguni, Nils, as- signor.) A/S Mekano. (Sec Schepeler, Herman A., assignor.) A/S Myrens Verkstad.(See Klling, Jens W. A., assignor.) A/S Stordo Kisgruber. (See Nielsen, C., and Ilelleland, assignors.) A-Z Company, The.'See llanmer, Laurence G., assignor.) Aagaard, Carl L., Rockford, 111. Hand scraping tool. No. 1,345,058 ; July 6; v. 276 ; p. 05. Aalborg, Christian, Wllkinsburg, Pa., assignor to Wcst- inghouse Electric and Manufacturing Company. Trol- ley.No. 1,334,943 ; Mar. 30 ; v. 272 ; p. 741. Aaron, Solomon E., Boston, Mass. Pliers. No. 1,329,155 ; Jan. 27 ; v. 270 ; p. 554. For instance, I would like to go to end of line and if last character is a comma or semicolon or hyphen then remove the CR. Then move line by line through the file and delete everything after a numerical sequence I am wondering if Python would be a good tool and if so where can I find information on how to accomplish this or would I be better off using something like the unix tool awk or something else?? Thanks Jay ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The whole Roman to Dec and vice versa issue
Hey, I'm a rather new programmer, but it seems to me the digital to roman should be coded: While Digital_Input > 0: If Digital_Input > 1000 then: Roman = + M, Digital_Input = - 1000 elif Digital_Input > 900 then: Roman = + C, Digital_Input = - 900 ... Now if someone could please clarify [or forward me to clarifications of-] separation of data from logics I should be very grateful. [Come to that, if someone could point me to a *simple* gui which I can use in python, keep in mind I did learn a little VB, I should be grateful as well.] Thank you. Omer Tabach. Quoting Bob Gailer : >> >> Digital to Roman pseudocode: >> >> 1. if digital_input is greater than 1000: >> subtract 1000 from it and add "M" to string roman_result >> # How do you do that, add one character to the end of an existing string? >> > Start with an empty string: > > roman_result = "" > > To add a character at the end: > > roman_result += "M" # Python shorthand for roman_result = roman_result + "M" > roman_result + "M" would also work, right/ I'm just trying to save time on typing in the code, right > >> # also, how do I modify the digital_input variable (it's an integer) > digital_input -= 1000 is that somewhat like digital_result = digital result - int(1000)? >> several times through the conversion process? >> > You will be processing the input in a loop (while or for). running = True and while running is how I've tended to set (my pseudocode) > As you gain familiarity with Python you will develop ways to separate > data from logic. I might say more about this later, but right now I'm > about to drive north a bit. > I have already gained some familiary with separating data manipulation (data and logic, as a whole) from the code for the user interface, that's something that oen of my advisors has been big on. - Now that's room service! Choose from over 150,000 hotels in 45,000 destinations on Yahoo! Travel to find your fit.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should I use python for parsing text
"Jay Mutter III" <[EMAIL PROTECTED]> wrote > See example next: > A.-C. Manufacturing Company. (See Sebastian, A. A., > and Capes, assignors.) >... >Aaron, Solomon E., Boston, Mass. Pliers. No. 1,329,155 ; >Jan. 27 ; v. 270 ; p. 554. > > For instance, I would like to go to end of line and if last > character is a comma or semicolon or hyphen then > remove the CR. It would look something like: output = open('example.fixed','w') for line in file('example.txt'): if line[-1] in ',;-':# check last character line = line.strip() # lose the C/R output.write(line)# write to output else: output.write(line) # append the next line complete with C/R output.close() > Then move line by line through the file and delete everything > after a numerical sequence Slightly more tricky because you need to use a regular expression. But if you know regex then only slightly. > I am wondering if Python would be a good tool Absolutely, its one of the areas where Python excels. > find information on how to accomplish this You could check my tutorial on the three topics: Handling text Handling files Regular Expressions. Also the standard python documentation for the general tutorial (assuming you've done basic programming in some other language before) plus the re module > using something like the unix tool awk or something else?? awk or sed could both be used, but Python is more generally useful so unless you already know awk I'd take the time to learn the basics of Python (a few hours maybe) and use that. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The whole Roman to Dec and vice versa issue
"Jaggo" <[EMAIL PROTECTED]> wrote > [Come to that, if someone could point me to a *simple* gui which > I can use in python, keep in mind I did learn a little VB, > I should be grateful as well.] Tkinter is the standard Python GUI (IDLE is built using it). Many prefer wxPython, but it's an extra download. You can see very basic GUI code for both in the GUI topic of my tutorial. Bear in mind that Python doesn't come with a GUI Form builder ala VB, although there are a few around. This is one of them (based on wxPython): http://pythoncard.sourceforge.net/ -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] problems to install python 2.5
Hello all I downloaded Python 2.5 (final release) from python.org. I am trying to install it on my Mac 0s 10.4 the current working version i have of python is 2.3 (also built as a framework) It goes through the "./configure" part ok, but the "make install" produces the following errors : Modules/posixmodule.c:22: warning: ignoring #pragma weak lchown Modules/posixmodule.c:23: warning: ignoring #pragma weak statvfs Modules/posixmodule.c:24: warning: ignoring #pragma weak fstatvfs Modules/posixmodule.c: In function `posix_lchown': Modules/posixmodule.c:1681: warning: implicit declaration of function `lchown' Modules/posixmodule.c: In function `initposix': Modules/posixmodule.c:8588: error: `fstatvfs' undeclared (first use in this function) Modules/posixmodule.c:8588: error: (Each undeclared identifier is reported only once Modules/posixmodule.c:8588: error: for each function it appears in.) Modules/posixmodule.c:8596: error: `statvfs' undeclared (first use in this function) Modules/posixmodule.c:8604: error: `lchown' undeclared (first use in this function) make: *** [Modules/posixmodule.o] Error 1 I can't find any documentation of such a problem on the net, does anyone have a clue to what might be the problem ? thanks! Tsila ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems to install python 2.5
I also tried that build as well - it says that installation completed succesfully - and when i try to launch IDLE - it won't open... On 3/11/07, Kent Johnson <[EMAIL PROTECTED]> wrote: Tsila Hassine wrote: > Hello all > > I downloaded Python 2.5 (final release) from python.org. I am trying to > install it on my Mac 0s 10.4 > > the current working version i have of python is 2.3 (also built as a > framework) > > > It goes through the "./configure" part ok, but the "make install" produces > the following errors : No clue about your errors, but you can download a framework build of Python 2.5 from http://www.pythonmac.org/packages/py25-fat/index.html You might have better luck with build questions on the python-mac list: http://www.python.org/community/sigs/current/pythonmac-sig/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems to install python 2.5
On 3/11/07, Tsila Hassine <[EMAIL PROTECTED]> wrote: > I also tried that build as well - it says that installation completed > succesfully - and when i try to launch IDLE - it won't open... > Could it be that the 2.3 version is still around and that you are trying to launch the 2.3 version of IDLE? This happened to me once. > On 3/11/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > Tsila Hassine wrote: > > > Hello all > > > > > > I downloaded Python 2.5 (final release) from python.org. I am trying to > > > install it on my Mac 0s 10.4 > > > > > > the current working version i have of python is 2.3 (also built as a > > > framework) > > > > > > > > > It goes through the "./configure" part ok, but the "make install" > produces > > > the following errors : > > > > No clue about your errors, but you can download a framework build of > > Python 2.5 from > > http://www.pythonmac.org/packages/py25-fat/index.html > > > > You might have better luck with build questions on the python-mac list: > > > http://www.python.org/community/sigs/current/pythonmac-sig/ > > > > Kent > > > > > ___ > 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] problems to install python 2.5
2.3 is still around indeed, but i launched the IDLE from the python 2.5folder, so i don't think that was the problem. on teh other hand - i discovered that when i type "python2.5" in teh terminal window it launches indeed the python 2.5, but when running a script from the command line it goes to the 2.3 version in the Frameworks. - so what do i need to do ? add it to the PATH ? how do i do that ? On 3/11/07, Andre Roberge <[EMAIL PROTECTED]> wrote: On 3/11/07, Tsila Hassine <[EMAIL PROTECTED]> wrote: > I also tried that build as well - it says that installation completed > succesfully - and when i try to launch IDLE - it won't open... > Could it be that the 2.3 version is still around and that you are trying to launch the 2.3 version of IDLE? This happened to me once. > On 3/11/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > Tsila Hassine wrote: > > > Hello all > > > > > > I downloaded Python 2.5 (final release) from python.org. I am trying to > > > install it on my Mac 0s 10.4 > > > > > > the current working version i have of python is 2.3 (also built as a > > > framework) > > > > > > > > > It goes through the "./configure" part ok, but the "make install" > produces > > > the following errors : > > > > No clue about your errors, but you can download a framework build of > > Python 2.5 from > > http://www.pythonmac.org/packages/py25-fat/index.html > > > > You might have better luck with build questions on the python-mac list: > > > http://www.python.org/community/sigs/current/pythonmac-sig/ > > > > Kent > > > > > ___ > 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] problems to install python 2.5
Tsila Hassine wrote: > Hello all > > I downloaded Python 2.5 (final release) from python.org. I am trying to > install it on my Mac 0s 10.4 > > the current working version i have of python is 2.3 (also built as a > framework) > > > It goes through the "./configure" part ok, but the "make install" produces > the following errors : No clue about your errors, but you can download a framework build of Python 2.5 from http://www.pythonmac.org/packages/py25-fat/index.html You might have better luck with build questions on the python-mac list: http://www.python.org/community/sigs/current/pythonmac-sig/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems to install python 2.5
Did you download the MacPython 2.5 distribution? You should find an "Update shell profile.command" file, which you can run. I believe once you do that, you will be set up so that IDLE will launch and you will default to version 2.5. On 3/11/07, Tsila Hassine <[EMAIL PROTECTED]> wrote: > Hello all > > I downloaded Python 2.5 (final release) from python.org. I am trying to > install it on my Mac 0s 10.4 > > the current working version i have of python is 2.3 (also built as a > framework) > > > It goes through the "./configure" part ok, but the "make install" produces > the following errors : > > > > Modules/posixmodule.c:22: warning: ignoring #pragma weak lchown > Modules/posixmodule.c:23: warning: ignoring #pragma weak statvfs > Modules/posixmodule.c:24: warning: ignoring #pragma weak fstatvfs > Modules/posixmodule.c: In function `posix_lchown': > Modules/posixmodule.c:1681: warning: implicit declaration of function > `lchown' > Modules/posixmodule.c: In function `initposix': > Modules/posixmodule.c:8588: error: `fstatvfs' undeclared (first use in this > function) > Modules/posixmodule.c:8588: error: (Each undeclared identifier is reported > only once > Modules/posixmodule.c:8588: error: for each function it appears in.) > Modules/posixmodule.c:8596: error: `statvfs' undeclared (first use in this > function) > Modules/posixmodule.c:8604: error: `lchown' undeclared (first use in this > function) > make: *** [Modules/posixmodule.o] Error 1 > > > I can't find any documentation of such a problem on the net, does anyone > have a clue to what might be the problem ? > > thanks! > Tsila > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- - - - - - - - Clay S. Wiedemann voice: 718.362.0375 aim: khlav wii: 3905 4571 6175 2469 twitter: seastokes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems to install python 2.5
yes I did download that, and it went " sorry can't update zshell" (and yes I have zshell) so i didn't know how to deal with that any ideas ? (but then i guess its a completely different problem then ...) thanks! T. On 3/11/07, Clay Wiedemann <[EMAIL PROTECTED]> wrote: Did you download the MacPython 2.5 distribution? You should find an "Update shell profile.command" file, which you can run. I believe once you do that, you will be set up so that IDLE will launch and you will default to version 2.5. On 3/11/07, Tsila Hassine <[EMAIL PROTECTED]> wrote: > Hello all > > I downloaded Python 2.5 (final release) from python.org. I am trying to > install it on my Mac 0s 10.4 > > the current working version i have of python is 2.3 (also built as a > framework) > > > It goes through the "./configure" part ok, but the "make install" produces > the following errors : > > > > Modules/posixmodule.c:22: warning: ignoring #pragma weak lchown > Modules/posixmodule.c:23: warning: ignoring #pragma weak statvfs > Modules/posixmodule.c:24: warning: ignoring #pragma weak fstatvfs > Modules/posixmodule.c: In function `posix_lchown': > Modules/posixmodule.c:1681: warning: implicit declaration of function > `lchown' > Modules/posixmodule.c: In function `initposix': > Modules/posixmodule.c:8588: error: `fstatvfs' undeclared (first use in this > function) > Modules/posixmodule.c:8588: error: (Each undeclared identifier is reported > only once > Modules/posixmodule.c:8588: error: for each function it appears in.) > Modules/posixmodule.c:8596: error: `statvfs' undeclared (first use in this > function) > Modules/posixmodule.c:8604: error: `lchown' undeclared (first use in this > function) > make: *** [Modules/posixmodule.o] Error 1 > > > I can't find any documentation of such a problem on the net, does anyone > have a clue to what might be the problem ? > > thanks! > Tsila > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- - - - - - - - Clay S. Wiedemann voice: 718.362.0375 aim: khlav wii: 3905 4571 6175 2469 twitter: seastokes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems to install python 2.5
No ideas there. I wish I could help, but I am new at this. I did stop using IDLE though in favor of iPython. I don't know if an end-around would solve all the problems you have been encountering though. http://ipython.scipy.org/moin/ -c On 3/11/07, Tsila Hassine <[EMAIL PROTECTED]> wrote: > yes I did download that, and it went " sorry can't update zshell" (and yes I > have zshell) so i didn't know how to deal with that any ideas ? (but > then i guess its a completely different problem then ...) > > thanks! > T. > > > On 3/11/07, Clay Wiedemann <[EMAIL PROTECTED]> wrote: > > Did you download the MacPython 2.5 distribution? You should find an > > "Update shell profile.command" file, which you can run. I believe once > > you do that, you will be set up so that IDLE will launch and you will > > default to version 2.5. > > > > On 3/11/07, Tsila Hassine <[EMAIL PROTECTED]> wrote: > > > Hello all > > > > > > I downloaded Python 2.5 (final release) from python.org. I am trying to > > > install it on my Mac 0s 10.4 > > > > > > the current working version i have of python is 2.3 (also built as a > > > framework) > > > > > > > > > It goes through the "./configure" part ok, but the "make install" > produces > > > the following errors : > > > > > > > > > > > > Modules/posixmodule.c:22: warning: ignoring #pragma weak lchown > > > Modules/posixmodule.c:23: warning: ignoring #pragma weak statvfs > > > Modules/posixmodule.c:24: warning: ignoring #pragma weak fstatvfs > > > Modules/posixmodule.c: In function `posix_lchown': > > > Modules/posixmodule.c:1681: warning: implicit declaration of function > > > `lchown' > > > Modules/posixmodule.c: In function `initposix': > > > Modules/posixmodule.c:8588: error: `fstatvfs' undeclared (first use in > this > > > function) > > > Modules/posixmodule.c:8588: error: (Each undeclared identifier is > reported > > > only once > > > Modules/posixmodule.c:8588: error: for each function it appears in.) > > > Modules/posixmodule.c:8596: error: `statvfs' undeclared (first use in > this > > > function) > > > Modules/posixmodule.c:8604: error: `lchown' undeclared (first use in > this > > > function) > > > make: *** [Modules/posixmodule.o] Error 1 > > > > > > > > > I can't find any documentation of such a problem on the net, does anyone > > > have a clue to what might be the problem ? > > > > > > thanks! > > > Tsila > > > > > > ___ > > > Tutor maillist - Tutor@python.org > > > http://mail.python.org/mailman/listinfo/tutor > > > > > > > > > > > > -- > > > > - - - - - - - > > > > Clay S. Wiedemann > > > > voice: 718.362.0375 > > aim: khlav > > wii: 3905 4571 6175 2469 > > twitter: seastokes > > > > -- - - - - - - - Clay S. Wiedemann voice: 718.362.0375 aim: khlav wii: 3905 4571 6175 2469 twitter: seastokes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The whole Roman to Dec and vice versa issue
On 12/03/07, Jaggo <[EMAIL PROTECTED]> wrote: > Hey, > I'm a rather new programmer, but it seems to me the digital to roman should > be coded: > While Digital_Input > 0: > If Digital_Input > 1000 then: Roman = + M, Digital_Input = - 1000 > elif Digital_Input > 900 then: Roman = + C, Digital_Input = - 900 > ... > Now if someone could please clarify [or forward me to clarifications of-] > separation of data from logics I should be very grateful. The idea is that our data is the information: 1000 corresponds to 'M' 500 corresponds to 'D' 100 corresponds to 'C' etc.. We can represent that in python using a dictionary: decToRomanData = { 1000:'M', 500:'D', 100:'C', 50:'L', 10:'X', 5:'V', 1:'I' } Now we need to write some code that will take an integer and use our data to convert to a Roman numeral. One way we could do that is: def decToRoman(dec): roman = '' while dec > 0: next = max(i for i in decToRomanData if dec >= i) roman += decToRomanData[next] dec -= next return roman Now, if we suddenly remember that the romans used 'Q' to represent 250, we can just edit our dictionary, and the rest of the code will remain the same. [note that this code will not produce strings like 'IV' for 4. OTOH, as I recall, the Romans didn't do that consistently either..] -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems to install python 2.5
Tsila Hassine wrote: > yes I did download that, and it went " sorry can't update zshell" (and > yes I > have zshell) so i didn't know how to deal with that any ideas ? (but > then i guess its a completely different problem then ...) All that program does is make sure that the bin directory included in the Python distribution is included in your path. For bash, it edits .bash_profile and inserts this line: export PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}" You should do the equivalent for zshell. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The whole Roman to Dec and vice versa issue
John Fouhy wrote: > On 12/03/07, Jaggo <[EMAIL PROTECTED]> wrote: >> Hey, >> I'm a rather new programmer, but it seems to me the digital to roman should >> be coded: >> While Digital_Input > 0: >> If Digital_Input > 1000 then: Roman = + M, Digital_Input = - 1000 >> elif Digital_Input > 900 then: Roman = + C, Digital_Input = - 900 >> ... >> Now if someone could please clarify [or forward me to clarifications of-] >> separation of data from logics I should be very grateful. > > The idea is that our data is the information: > 1000 corresponds to 'M' > 500 corresponds to 'D' > 100 corresponds to 'C' > etc.. > > We can represent that in python using a dictionary: > > decToRomanData = { 1000:'M', 500:'D', 100:'C', 50:'L', 10:'X', 5:'V', 1:'I' } A list would probably work better here because the order is significant and random lookup is not. > > Now we need to write some code that will take an integer and use our > data to convert to a Roman numeral. One way we could do that is: > > def decToRoman(dec): > roman = '' > while dec > 0: > next = max(i for i in decToRomanData if dec >= i) > roman += decToRomanData[next] > dec -= next > return roman Remember this thread started as a homework problem...we shouldn't give solutions until the OP has one already. Then we can all get clever and show how we would have done it :-) > > Now, if we suddenly remember that the romans used 'Q' to represent > 250, we can just edit our dictionary, and the rest of the code will > remain the same. > > [note that this code will not produce strings like 'IV' for 4. OTOH, > as I recall, the Romans didn't do that consistently either..] It seems to me that isn't so different from deciding to use 'Q' for 250. Maybe you can change the dictionary for this one too. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The whole Roman to Dec and vice versa issue
On 12/03/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > > [note that this code will not produce strings like 'IV' for 4. OTOH, > > as I recall, the Romans didn't do that consistently either..] > It seems to me that isn't so different from deciding to use 'Q' for 250. > Maybe you can change the dictionary for this one too. Ahh, good point. I was thinking in terms of extra logic to figure out the subtraction rules, but you could just use extra symbols :-) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to send command line args to py script
lo there all, i was wondering how to make a python script accept command line arguments. i mean, i have used python scripts from the command line in linux and passed something to it and it knows what to do. like in a function, if i want to do something like this def add_two_numbers(a, b): x = a + b return x x = add_two_numbers(4,5) print x how could i do the same from the cli. like python add_two_numbers.py 4 5 or maybe python add_two_numbers.py 4, 5 or even python add_two_numbers.py -a 4 -b 5 is there an easy way to do this ? thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Roman Numeral - To - Digital Converter
Roman Numeral - To - Digital Converter So far, I'm coming along OK with the code that runs the conversions, assuming the user gives me a proper input. (A string of capital letters I, V, X, L, C, D, M) However, not every input someone gives the function is goign to be properly formatted like that. So: 1. How do I have my function "screen" an input string for characters that aren't I,V,X,L,C,D or M? 2. If any of the characters in the string are merely lowercase i,v,x,l,c,d, or m, how would I tell Python to capitalize those and go on with working the function? -- "Blind faith in bad leadership is not patriotism." "One of the most horrible features of war is that all the war-propaganda, all the screaming and lies and hatred, comes invariably from people who are not fighting."-George Orwell, _Homage to Catalonia ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to send command line args to py script
This is my first time replying to the list, so excuse me if this goes out wrong. Anyhow, you're looking for sys.agrv. sys.agrv is a list of the arguments, with sys.agrv[0] being the script name. Code: ## import sys #sys.argv is part of the sys module def add_two_numbers(a, b): x = a + b return x y = float(sys.argv[1]) #This gets the arguments and converts them to numbers z = float(sys.argv[2]) #You need to do this because the arguments come as strings x = add_two_numbers(y,z) #Of course, that means if you put in "python add.py 1 fish" print x #you'll get an error. So a try-except clause would be good here. ## Run the above script with the formatting "python (filename).py 1 1", not "python (filename).py -1 -1", that'll get you negative numbers. shawn bright wrote: > lo there all, > > i was wondering how to make a python script accept command line arguments. > i mean, i have used python scripts from the command line in linux and > passed something to it and it knows what to do. > > like in a function, if i want to do something like this > > def add_two_numbers(a, b): > x = a + b > return x > > x = add_two_numbers(4,5) > print x > > how could i do the same from the cli. > > like python add_two_numbers.py 4 5 > or maybe python add_two_numbers.py 4, 5 > or even python add_two_numbers.py -a 4 -b 5 > > is there an easy way to do this ? > > thanks > ___ > 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] String-manipulation question
#start of triple-letter processing block if len(roman_input) > 2 and roman_input[0] + roman_input[1] + roman_input[2] == "MMM": digital_result += 3000 roman_input = #something to remove those "M"'s from the string continue -- My question here boils down to, "Is there a way to remove certain characters from a string?" Here, I'm trying to set the variable roman_input (the string's name) equal to the string minus those characters, the 3 Ms. (being the first three characters of the string, they would be roman_input[0], roman_input[1], and roman_input[2].) Setting the (new) string equal to the (old) string minus those three characters will enable calculations on the string to keep running, because I have this piece of code, and similar pieces nested in a loop. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to send command line args to py script
Well, for your first response, its great, thanks a lot. shawn On 3/11/07, Gordon <[EMAIL PROTECTED]> wrote: > This is my first time replying to the list, so excuse me if this goes > out wrong. > > Anyhow, you're looking for sys.agrv. sys.agrv is a list of the > arguments, with sys.agrv[0] being the script name. > > Code: > ## > import sys #sys.argv is part of the sys module > def add_two_numbers(a, b): > x = a + b > return x > y = float(sys.argv[1]) #This gets the arguments and converts them to numbers > z = float(sys.argv[2]) #You need to do this because the arguments come > as strings > x = add_two_numbers(y,z) #Of course, that means if you put in "python > add.py 1 fish" > print x #you'll get an error. So a try-except clause would be good here. > ## > Run the above script with the formatting "python (filename).py 1 1", not > "python (filename).py -1 -1", that'll get you negative numbers. > > shawn bright wrote: > > lo there all, > > > > i was wondering how to make a python script accept command line arguments. > > i mean, i have used python scripts from the command line in linux and > > passed something to it and it knows what to do. > > > > like in a function, if i want to do something like this > > > > def add_two_numbers(a, b): > > x = a + b > > return x > > > > x = add_two_numbers(4,5) > > print x > > > > how could i do the same from the cli. > > > > like python add_two_numbers.py 4 5 > > or maybe python add_two_numbers.py 4, 5 > > or even python add_two_numbers.py -a 4 -b 5 > > > > is there an easy way to do this ? > > > > thanks > > ___ > > 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] Roman Numeral - To - Digital Converter
Alan Gilfoy wrote: > Roman Numeral - To - Digital Converter > > So far, I'm coming along OK with the code that runs the conversions, > assuming the user gives me a proper input. (A string of capital > letters I, V, X, L, C, D, M) > > However, not every input someone gives the function is goign to be > properly formatted like that. > > So: > > 1. How do I have my function "screen" an input string for characters > that aren't I,V,X,L,C,D or M? > 2. If any of the characters in the string are merely lowercase > i,v,x,l,c,d, or m, how would I tell Python to capitalize those and go > on with working the function? > >>> 'heLlo'.upper() 'HELLO' >>> print 'i' in 'hilly' True >>> print 'i' in 'holy' False I hope that gives you some clues. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String-manipulation question
On 12/03/07, Alan Gilfoy <[EMAIL PROTECTED]> wrote: > My question here boils down to, "Is there a way to remove certain > characters from a string?" There are several ways, depending on what exactly you want to achieve. In this case, it looks like you're after "string slicing". The tutorial covers this; look about half-way down the page: http://docs.python.org/tut/node5.html HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to send command line args to py script
On Sun, Mar 11, 2007 at 09:07:41PM -0500, shawn bright wrote: > Well, for your first response, its great, > thanks a lot. > shawn And, when you needs to process command line arguments and flags and options become more complicated, be sure to look at modules getopt and optparse in the standard library. optparse is newer and more powerful than getopt, but also seems a bit more complex. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to send command line args to py script
OK, will do. Looks like my future will involve more of this kind of thing. thanks shawn On 3/11/07, Dave Kuhlman <[EMAIL PROTECTED]> wrote: > On Sun, Mar 11, 2007 at 09:07:41PM -0500, shawn bright wrote: > > Well, for your first response, its great, > > thanks a lot. > > shawn > > And, when you needs to process command line arguments and flags and > options become more complicated, be sure to look at modules getopt > and optparse in the standard library. optparse is newer and more > powerful than getopt, but also seems a bit more complex. > > Dave > > > -- > Dave Kuhlman > http://www.rexx.com/~dkuhlman > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor