What use with Python
Hi, I am new to Python. Weeks ago, I was asked about Python questions on an interview. Now I want to learn Python, but I do not know what I can do with it on a PC. Especially I would like to do something interesting instead of some text search etc. Python may can do more than I realize now. Could you tell me something on a PC? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
This Python script cannot open by a editor?
Hi, I am learning a Python Tool from web: http://www.ohwr.org/projects/hdl-make/wiki/Quick-start-new I download the program to Ubuntu 12.04. I find that in the folder it is shown as hdlmake-v1.0, 37.8 KB Python Script. I remember that script file can be loaded to an editor to read its content. But, when I open it by double click, it is shown as like binary file in GEDIT. What is the reason of this? A general Python file has .py extension, and can be edited. Is it right? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Question about metacharacter '*'
Hi, I just begin to learn Python. I do not see the usefulness of '*' in its description below: The first metacharacter for repeating things that we'll look at is *. * doesn't match the literal character *; instead, it specifies that the previous character can be matched zero or more times, instead of exactly once. For example, ca*t will match ct (0 a characters), cat (1 a), caaat (3 a characters), and so forth. It has to be used with other search constraints? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Why is it different from the example on the tutorial?
Hi, I type the following sample codes on Python, but it echoes differently. Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. import re p = re.compile('ab*') p _sre.SRE_Pattern object at 0x... What I get on Python console: $ python Python 2.7.5 (default, Oct 2 2013, 22:34:09) [GCC 4.8.1] on cygwin Type help, copyright, credits or license for more information. import re p = re.compile('ab*') File stdin, line 1 p = re.compile('ab*') ^ SyntaxError: invalid syntax Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 8:54:42 AM UTC-4, Tim Chase wrote: On 2014-07-06 05:13, rxjw...@gmail.com wrote: What I get on Python console: $ python Python 2.7.5 (default, Oct 2 2013, 22:34:09) [GCC 4.8.1] on cygwin Type help, copyright, credits or license for more information. import re p = re.compile('ab*') File stdin, line 1 p = re.compile('ab*') ^ SyntaxError: invalid syntax Are you sure that you copied/pasted that directly from the console instead of transcribing it with some mistake? I just did the same thing at the console and it worked perfectly fine $ python Python 2.7.3 (default, Mar 13 2014, 11:03:55) [GCC 4.7.2] on linux2 Type help, copyright, credits or license for more information. import re p = re.compile('ab*') -tkc Thanks. It did be caused by unclear copypaste. I shall be careful in future. When I enter: counter=100 counter 100 When I get match result: pattern='abcd' prog = re.compile(pattern) string='abcd' result = prog.match(string) result _sre.SRE_Match object at 0x6eda5e0 result.group(0) 'abcd' It looks like 'result' is different from a simple 'counter' variable. I do not yet find the definition of 'result' object. What do you call 'result' object? Where can I find it (what topic would be in a tutorial)? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 10:18:53 AM UTC-4, Rick Johnson wrote: On Sunday, July 6, 2014 8:38:41 AM UTC-5, rxj...@gmail.com wrote: When I get match result: pypattern='abcd' pyprog = re.compile(pattern) pystring='abcd' pyresult = prog.match(string) pyresult _sre.SRE_Match object at 0x6eda5e0 pyresult.group(0) 'abcd' It looks like 'result' is different from a simple 'counter' variable. I do not yet find the definition of 'result' object. What do you call 'result' object? Where can I find it (what topic would be in a tutorial)? Thanks, One of the most powerful features of Python,,, for the noob,,, be documentation strings. With Python you need not buy expensive books, or venture into seedy and dangerous alley ways of the inter-webs, no, all you need to do is do that which any young and inexperienced lad would do when he finds himself in a troubling situation: YELL FOR HELP! The built-in function help will answer all your questions,,, considering you ask the correct questions of course!,,, but always remember the advice of a wise man and don't become the boy who cried wolf one too many times! Thanks. I do not want to waste everyone's time. For a jump start, there are small errors making me frustrating. Your help does help me, confirm the usage etc. After a basic familiarity, I do not want to post more. I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. (Sorry again, that problem may be about Cygwin, not Python. Excuse me to mention that here. Of course, your help post consumes your time. Thanks again. -- https://mail.python.org/mailman/listinfo/python-list
What is the difference between matchObj.group() and matchObj.group(0)
Hi, I cannot get the difference between matchObj.group() and matchObj.group(0), Although there definitions are obvious different. And group() mentions 'tuple'. tuple means all the elements in line object? Match Object Methods Description group(num=0) This method returns entire match (or specific subgroup num) groups() This method returns all matching subgroups in a tuple (empty if there weren't any) I run the following code. Even I add more words to line object, Both have the same output. Could you clarify this question? Thanks again. #!/usr/bin/python import re line = Cats are smarter than dogs matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) if matchObj: print matchObj.group() : , matchObj.group() print matchObj.group(1) : , matchObj.group(1) print matchObj.group(2) : , matchObj.group(2) else: print No match!! -- https://mail.python.org/mailman/listinfo/python-list
How to write this repeat matching?
Hi, On Python website, it says that the following match can reach 'abcb' in 6 steps: . A step-by-step example will make this more obvious. Let's consider the expression a[bcd]*b. This matches the letter 'a', zero or more letters from the class [bcd], and finally ends with a 'b'. Now imagine matching this RE against the string abcbd. The end of the RE has now been reached, and it has matched abcb. This demonstrates how the matching engine goes as far as it can at first, and if no match is found it will then progressively back up and retry the rest of the RE again and again. It will back up until it has tried zero matches for [bcd]*, and if that subsequently fails, the engine will conclude that the string doesn't match the RE at all. . I write the following code: ... import re line = abcdb matchObj = re.match( 'a[bcd]*b', line) if matchObj: print matchObj.group() : , matchObj.group() print matchObj.group(0) : , matchObj.group() print matchObj.group(1) : , matchObj.group(1) print matchObj.group(2) : , matchObj.group(2) else: print No match!! . In which I have used its match pattern, but the result is not 'abcb' Only matchObj.group(0): abcdb displays. All other group(s) have no content. How to write this greedy search? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 4:32:14 PM UTC-4, Larry Hudson wrote: On 07/06/2014 08:03 AM, rxjw...@gmail.com wrote: snip Thanks. I do not want to waste everyone's time. For a jump start, there are small errors making me frustrating. Your help does help me, confirm the usage etc. After a basic familiarity, I do not want to post more. I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. (Sorry again, that problem may be about Cygwin, not Python. Excuse me to mention that here. Of course, your help post consumes your time. Thanks again. I'm curious as to why you're using Cygwin Python. Cygwin is great for using Unix/Linux programs that are not available in Windows, but there are Windows-native versions of Python available. I would suggest that you would be better off installing Python directly into Windows instead of going round-about with Cygwin. If you do this, I would also suggest that you install Python 3 instead of Python 2, but you may have to find a different tutorial. Although I suspect that part of your problems are that the tutorial you're using IS for Python 3 rather than the 2.7 you are using. Really, there aren't a lot of differences between 2 and 3, but the differences ARE very significant. And 3 is definitely the better, as well as the future. (Aside: I do have Cygwin installed on my Windows system, but I have pretty much given up on using Windows. I find Linux far superior and more comfortable to use, and it gets away from Big-Brotherish Microsoft. I do use it occasionally, but I think the last time I ran it was at least two months ago.) -=- Larry -=- Thanks. In fact, I have both Cygwin and Windows version Python, but I incorrectly thought that Cygwin version Python would be closer to Linux than Windows Python. Another thing, I find that Windows Python have a GUI interface. It is good, but it does not have command history as Cygwin has. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to write this repeat matching?
On Sunday, July 6, 2014 3:26:44 PM UTC-4, Ian wrote: On Sun, Jul 6, 2014 at 12:57 PM, rxjw...@gmail.com wrote: I write the following code: ... import re line = abcdb matchObj = re.match( 'a[bcd]*b', line) if matchObj: print matchObj.group() : , matchObj.group() print matchObj.group(0) : , matchObj.group() print matchObj.group(1) : , matchObj.group(1) print matchObj.group(2) : , matchObj.group(2) else: print No match!! . In which I have used its match pattern, but the result is not 'abcb' You're never going to get a match of 'abcb' on that string, because 'abcb' is not found anywhere in that string. There are two possible matches for the given pattern over that string: 'abcdb' and 'ab'. The first one matches the [bcd]* three times, and the second one matches it zero times. Because the matching is greedy, you get the result that matches three times. It cannot match one, two or four times because then there would be no 'b' following the [bcd]* portion as required by the pattern. Only matchObj.group(0): abcdb displays. All other group(s) have no content. Calling match.group(0) is equivalent to calling match.group without arguments. In that case it returns the matched string of the entire regular expression. match.group(1) and match.group(2) will return the value of the first and second matching group respectively, but the pattern does not have any matching groups. If you want a matching group, then enclose the part that you want it to match in parentheses. For example, if you change the pattern to: matchObj = re.match('a([bcd]*)b', line) then the value of matchObj.group(1) will be 'bcd' Because I am new to Python, I may not describe the question clearly. Could you read the original problem on web: https://docs.python.org/2/howto/regex.html It says that it gets 'abcb'. Could you explain it to me? Thanks again A step-by-step example will make this more obvious. Let's consider the expression a[bcd]*b. This matches the letter 'a', zero or more letters from the class [bcd], and finally ends with a 'b'. Now imagine matching this RE against the string abcbd. Step Matched Explanation 1 a The a in the RE matches. 2 abcbd The engine matches [bcd]*, going as far as it can, which is to the end of the string. 3 Failure The engine tries to match b, but the current position is at the end of the string, so it fails. 4 abcb Back up, so that [bcd]* matches one less character. 5 Failure Try b again, but the current position is at the last character, which is a 'd'. 6 abc Back up again, so that [bcd]* is only matching bc. 6 abcb Try b again. This time the character at the current position is 'b', so it succeeds. -- https://mail.python.org/mailman/listinfo/python-list
What is 're.M'?
Hi, I learn this short Python code from: http://www.tutorialspoint.com/python/python_reg_expressions.htm but I still do not decipher the meaning in its line, even after read its command explanation. It says that: re.M: Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string). More specific, what does 're.M' means? I have tried several other modification to the searchObj line, without clue yet. Could you explain re.M and the following two searchObj mechanisms? Thanks, import re line = Cats are smarter than dogs; searchObj = re.search( r'(.*) (.*?) .*', line, re.M|re.I) # searchObj = re.search( r'(.*) (.*?) .*', line, re.M|re.I) if searchObj: print searchObj.group() : , searchObj.group() print searchObj.group(1) : , searchObj.group(1) print searchObj.group(2) : , searchObj.group(2) else: print Nothing found!! -- https://mail.python.org/mailman/listinfo/python-list
Re: What is 're.M'?
On Monday, July 7, 2014 10:46:19 AM UTC-4, Steven D'Aprano wrote: On Mon, 07 Jul 2014 07:08:53 -0700, rxjwg98 wrote: More specific, what does 're.M' means? Feel free to look at it interactively. re.M is a flag to control the meaning of the regular expression. It is short for re.MULTILINE, just as re.I is short for re.IGNORECASE: py import re py re.M == re.MULTILINE True py re.I == re.IGNORECASE True They are just numeric flags: py re.I 2 py re.M 8 so you can combine then: py re.I | re.M 10 re.M turns on multi-line matching. This changes the meaning of the special characters ^ and $. Standard mode: ^ matches the start of the string $ matches the end of the string Multi-line mode: ^ matches the start of each line $ matches the end of each line Here is an example. Copy and paste this into the interpreter: import re text = First line. Second line. Third line. pattern = ^.*$ # Match anything from the start to end. By default, . does not match newlines, so by default the regex matches nothing: py re.search(pattern, text) is None # Nothing matches! True If you use MULTILINE mode, $ matches the end of the first line: py re.search(pattern, text, re.M).group() 'First line.' If you add MULTILINE mode and DOTALL mode, it matches everything: py re.search(pattern, text, re.M|re.S).group() 'First line.\nSecond line.\nThird line.' See the reference manual for more details: https://docs.python.org/3/library/re.html#module-contents (Python 3) https://docs.python.org/2/library/re.html#module-contents (Python 2) -- Steven Thanks all of you. The email address associates with this group is not my everyday email. Excuse me for not reply yet. I did read for several times the part you point out. I admit, that I have difficulties on reading now. In that explanation, it says about '$' and '^'. But I do not see any '$' and '^' in r'(.*) are (.*?) .*' '$' and '^' are pattern characters? It should be inside matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) How to connect '$' or '^' to: matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) Thanks for your help and please correct me now. -- https://mail.python.org/mailman/listinfo/python-list
Re: What is 're.M'?
On Monday, July 7, 2014 10:46:19 AM UTC-4, Steven D'Aprano wrote: On Mon, 07 Jul 2014 07:08:53 -0700, rxjwg98 wrote: More specific, what does 're.M' means? Feel free to look at it interactively. re.M is a flag to control the meaning of the regular expression. It is short for re.MULTILINE, just as re.I is short for re.IGNORECASE: py import re py re.M == re.MULTILINE True py re.I == re.IGNORECASE True They are just numeric flags: py re.I 2 py re.M 8 so you can combine then: py re.I | re.M 10 re.M turns on multi-line matching. This changes the meaning of the special characters ^ and $. Standard mode: ^ matches the start of the string $ matches the end of the string Multi-line mode: ^ matches the start of each line $ matches the end of each line Here is an example. Copy and paste this into the interpreter: import re text = First line. Second line. Third line. pattern = ^.*$ # Match anything from the start to end. By default, . does not match newlines, so by default the regex matches nothing: py re.search(pattern, text) is None # Nothing matches! True If you use MULTILINE mode, $ matches the end of the first line: py re.search(pattern, text, re.M).group() 'First line.' If you add MULTILINE mode and DOTALL mode, it matches everything: py re.search(pattern, text, re.M|re.S).group() 'First line.\nSecond line.\nThird line.' See the reference manual for more details: https://docs.python.org/3/library/re.html#module-contents (Python 3) https://docs.python.org/2/library/re.html#module-contents (Python 2) -- Steven Thanks, your post makes me much clearer. But for my original example, it has only one line: matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) re.M does not make any difference, is it right? -- https://mail.python.org/mailman/listinfo/python-list
Re: What is 're.M'?
On Monday, July 7, 2014 10:46:19 AM UTC-4, Steven D'Aprano wrote: On Mon, 07 Jul 2014 07:08:53 -0700, rxjwg98 wrote: More specific, what does 're.M' means? Feel free to look at it interactively. re.M is a flag to control the meaning of the regular expression. It is short for re.MULTILINE, just as re.I is short for re.IGNORECASE: py import re py re.M == re.MULTILINE True py re.I == re.IGNORECASE True They are just numeric flags: py re.I 2 py re.M 8 so you can combine then: py re.I | re.M 10 re.M turns on multi-line matching. This changes the meaning of the special characters ^ and $. Standard mode: ^ matches the start of the string $ matches the end of the string Multi-line mode: ^ matches the start of each line $ matches the end of each line Here is an example. Copy and paste this into the interpreter: import re text = First line. Second line. Third line. pattern = ^.*$ # Match anything from the start to end. By default, . does not match newlines, so by default the regex matches nothing: py re.search(pattern, text) is None # Nothing matches! True If you use MULTILINE mode, $ matches the end of the first line: py re.search(pattern, text, re.M).group() 'First line.' If you add MULTILINE mode and DOTALL mode, it matches everything: py re.search(pattern, text, re.M|re.S).group() 'First line.\nSecond line.\nThird line.' See the reference manual for more details: https://docs.python.org/3/library/re.html#module-contents (Python 3) https://docs.python.org/2/library/re.html#module-contents (Python 2) -- Steven Your patter is: pattern = ^.*$ while my example has pattern: '(.*) are (.*?) .*' which does not have either '^' or '$' Why re.M has effects on my example? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Question about metacharacter '*'
On Sunday, July 6, 2014 8:09:57 AM UTC-4, Devin Jeanpierre wrote: On Sun, Jul 6, 2014 at 4:51 AM, rxjw...@gmail.com wrote: Hi, I just begin to learn Python. I do not see the usefulness of '*' in its description below: The first metacharacter for repeating things that we'll look at is *. * doesn't match the literal character *; instead, it specifies that the previous character can be matched zero or more times, instead of exactly once. For example, ca*t will match ct (0 a characters), cat (1 a), caaat (3 a characters), and so forth. It has to be used with other search constraints? (BTW, this is a regexp question, not really a Python question per se.) That's usually when it's useful, yeah. For example, [0-9] matches any of the characters 0 through 9. So to match a natural number written in decimal form, we might use the regexp [0-9][0-9]*, which matches the strings 1, 12, and 007, but not or Jeffrey. Another useful one is `.*` -- `.` matches exactly one character, no matter what that character is. So, `.*` matches any string at all. The power of regexps stems from the ability to mix and match all of the regexp pieces in pretty much any way you want. -- Devin Would you give me an example using your pattern: `.*` -- `.`? I try it, but it cannot pass. (of course, I use it incorrectly) -- https://mail.python.org/mailman/listinfo/python-list
How to write match nth grouped subexpression?
Hi, It says that: match checks for a match only at the beginning of the string. Then, it also says that: \1...\9Matches nth grouped subexpression. I don't know how to write a script to include grouped subexpression in match? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Why is it different about '\s' Matches whitespace and Equivalent to [\t\n\r\f]?
Hi, On a tutorial it says that '\s': Matches whitespace. Equivalent to [\t\n\r\f]. I test it with: re.match(r'\s*\d\d*$', ' 111') _sre.SRE_Match object at 0x03642BB8 re.match(r'\t\n\r\f*\d\d*$', ' 111')# fails re.match(r'[\t\n\r\f]*\d\d*$', ' 111') # fails re.match(r'[\t\n\r\f]\d\d*$', ' 111') # fails re.match(r'[\t\n\r\f]*$', ' 111') # fails What is wrong in above script? Thanks -- https://mail.python.org/mailman/listinfo/python-list