Re: [Tutor] Regex for Filesystem path (Asad)

2018-11-08 Thread Peter Otten
Albert-Jan Roskam wrote: > I was thinking it would also be possible to do (in Windows): > import os.path > os.path.sep = '/' > os.path.normpath('c:\\beeh/foo\\bar/baz') > > But alas, this still creates normalized windows-style paths. If your input data has only forward slashes you can keep it th

Re: [Tutor] Regex for Filesystem path (Asad)

2018-11-08 Thread Albert-Jan Roskam
From: Tutor on behalf of Alan Gauld via Tutor Sent: Thursday, November 8, 2018 10:49 AM To: tutor@python.org Subject: Re: [Tutor] Regex for Filesystem path (Asad) On 08/11/2018 02:55, Asad wrote: > Why is it putting \ this breaks the unix path it should be: > > /a/b

Re: [Tutor] Regex for Filesystem path (Asad)

2018-11-08 Thread Alan Gauld via Tutor
On 08/11/2018 02:55, Asad wrote: > Why is it putting \ this breaks the unix path it should be: > > /a/b/c/d/test/28163133/22326541 ===> for unix platform logs > > \a\b\c\d\test\28163133\22326541 ===> for windows platform logs os.path.join uses the separator that is correct for your OS. Sinc

Re: [Tutor] Regex for Filesystem path (Asad)

2018-11-08 Thread Asad
uld > To: tutor@python.org > Cc: > Bcc: > Date: Wed, 7 Nov 2018 19:21:58 + > Subject: Re: [Tutor] Regex for Filesystem path (Asad) > On 07/11/2018 15:56, Asad wrote: > > Hi All, > > > > I tired seems its not working as required : > > > &

Re: [Tutor] Regex for Filesystem path (Asad)

2018-11-07 Thread Alan Gauld via Tutor
On 07/11/2018 15:56, Asad wrote: > Hi All, > > I tired seems its not working as required : > > from os.path import dirname, join > > testdir = dirname("/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log") Note that this will set testdir to /a/b/c/d/test/test_2814__2018_10_05_12_12_

Re: [Tutor] Regex for Filesystem path (Asad)

2018-11-07 Thread Asad
23456/789 Instead i need the script to go to the location : /a/b/c/d/test/123456/789 Please advice . Thanks, > -- Forwarded message -- > From: Cameron Simpson > To: tutor@python.org > Cc: > Bcc: > Date: Wed, 7 Nov 2018 06:47:33 +1100 > Subject: R

Re: [Tutor] Regex for Filesystem path

2018-11-06 Thread Sarfraaz Ahmed
Hello, There are specific operating system, path related modules in Python for handling these scenarios. You could try looking at os.path module. On Tue, Nov 6, 2018 at 11:16 PM Asad wrote: > Hi all , > > Can you provide some advice and code for the following problem : > > I have a log

Re: [Tutor] Regex for Filesystem path

2018-11-06 Thread Alan Gauld via Tutor
On 06/11/2018 19:47, Cameron Simpson wrote: > It is better to just construct the required path. Chdir there requires a > chdir back, and chdir affects all the relative paths your programme may > be using. > > I'd use os.path.dirname to get '/a/b/c/d/test' and then just append to > it with os.p

Re: [Tutor] Regex for Filesystem path

2018-11-06 Thread Cameron Simpson
On 06Nov2018 18:10, Alan Gauld wrote: >On 06/11/2018 13:13, Asad wrote: > >> Can you provide some advice and code for the following problem : > >The first thing is to go read the documentation for the os.path module. >It is designed for reliable path manipulation. > >> /a/b/c/d/test/test_2

Re: [Tutor] Regex for Filesystem path

2018-11-06 Thread Mats Wichmann
>>4) look for the latest file in the directory /a/b/c/d/test/123456/789 > > Slightly more complex, you need the creation timestamp. > You can find that with os.path.getctime() (or several > other options, eg os.stat) here's a trick you might be able to make use of: somelist = generate-lis

Re: [Tutor] Regex for Filesystem path

2018-11-06 Thread Alan Gauld via Tutor
On 06/11/2018 13:13, Asad wrote: > Can you provide some advice and code for the following problem : The first thing is to go read the documentation for the os.path module. It is designed for reliable path manipulation. > /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log > > f3 = ope

[Tutor] Regex for Filesystem path

2018-11-06 Thread Asad
Hi all , Can you provide some advice and code for the following problem : I have a logfile to check for errors : /a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' ) st1 = f3.readlines () from the above log I

Re: [Tutor] Regex not working as desired

2018-03-06 Thread Alan Gauld via Tutor
On 06/03/18 22:17, Albert-Jan Roskam wrote: > But the way you wrote it, the generator expression just "floats" Any expression can be used where a value is expected provided that e3xpression produces a value of the required type. A generator expression effectively produces a sequence and the type

Re: [Tutor] Regex not working as desired

2018-03-06 Thread Steven D'Aprano
On Tue, Mar 06, 2018 at 10:17:20PM +, Albert-Jan Roskam wrote: > > >>> all(c.isdigit() for c in '12c4') > > False > > I never understood why this is syntactically correct. It's like two > parentheses are missing. > > This I understand: > all((c.isdigit() for c in '12c4')) > Or this: > all([

Re: [Tutor] Regex not working as desired

2018-03-06 Thread Albert-Jan Roskam
On Feb 27, 2018 09:50, Alan Gauld via Tutor wrote: > > On 27/02/18 05:13, Cameron Simpson wrote: > > > hard to debug when you do. That's not to say you shouldn't use them, but > > many > > people use them for far too much. > > > > Finally, you could also consider not using a regexp for this part

Re: [Tutor] Regex not working as desired

2018-02-27 Thread Alan Gauld via Tutor
On 27/02/18 09:50, Peter Otten wrote: >> def all_digits(s): >> return all(c.isdigit() for c in s) > > Note that isdigit() already checks all characters in the string: Ah! I should have known that but forgot. I think the singular name confused me. > The only difference to your suggestion is

Re: [Tutor] Regex not working as desired

2018-02-27 Thread Peter Otten
Alan Gauld via Tutor wrote: > On 27/02/18 05:13, Cameron Simpson wrote: > >> hard to debug when you do. That's not to say you shouldn't use them, but >> many people use them for far too much. > > >> Finally, you could also consider not using a regexp for this particular >> task. Python's "int"

Re: [Tutor] Regex not working as desired

2018-02-27 Thread Alan Gauld via Tutor
On 27/02/18 05:13, Cameron Simpson wrote: > hard to debug when you do. That's not to say you shouldn't use them, but many > people use them for far too much. > Finally, you could also consider not using a regexp for this particular task. > > Python's "int" class can be called with a string,

Re: [Tutor] Regex not working as desired

2018-02-27 Thread Steven D'Aprano
On Mon, Feb 26, 2018 at 11:01:49AM -0800, Roger Lea Scherer wrote: > The first step is to input data and then I want to check to make sure > there are only digits and no other type of characters. I thought regex > would be great for this. I'm going to quote Jamie Zawinski: Some people, when

Re: [Tutor] Regex not working as desired

2018-02-27 Thread Terry Carroll
On Mon, 26 Feb 2018, Terry Carroll wrote: Instead of looking fo re xcaprions.. Wow. That should read "Instead of looking for exceptions..." Something really got away from me there. -- Terry Carroll carr...@tjc.com ___ Tutor maillist - Tutor@pyth

Re: [Tutor] Regex not working as desired

2018-02-27 Thread Terry Carroll
On Mon, 26 Feb 2018, Roger Lea Scherer wrote: """ ensure input is no other characters than digits sudocode: if the input has anything other than digits return digits """ p = re.compile(r'[^\D]') I'm not so great at regular expressions, but this regex appears to be searching for a str

Re: [Tutor] Regex not working as desired

2018-02-26 Thread Cameron Simpson
On 26Feb2018 11:01, Roger Lea Scherer wrote: The first step is to input data and then I want to check to make sure there are only digits and no other type of characters. I thought regex would be great for this. Many people do :-) They are a reasonable tool for an assortment of text matching

[Tutor] Regex not working as desired

2018-02-26 Thread Roger Lea Scherer
The first step is to input data and then I want to check to make sure there are only digits and no other type of characters. I thought regex would be great for this. The program works great, but no matter what I enter, the regex part does the same thing. By same thing I mean this: RESTA

Re: [Tutor] Regex ^$ not behaving as expected

2016-12-08 Thread Edmund Butterworth via Tutor
asons I want to do this in Python. I can and shall now. Best regards Ed Original Message *Subject: *Re: [Tutor] Regex ^$ not behaving as expected *From: *Danny Yoo *To: *Edmund Butterworth *Cc: *Python Tutor Mailing List *Date: *08/12/2016, 17:20:19 Following up:

Re: [Tutor] Regex ^$ not behaving as expected

2016-12-08 Thread Danny Yoo
Following up: drats! Detecting this conceptual TypeError is not feasible under the current design, due to the choice of data representation used in this API. The reason is because the flags are being represented as integers, and we're using bitwise operations to define the union of flags. That i

Re: [Tutor] Regex ^$ not behaving as expected

2016-12-08 Thread Danny Yoo
> I'm still somewhat confused as to what the regexp module is doing when > passing a non-numeric count parameter. That looks like it should > raise a TypeError to me, so perhaps someone needs to file a bug > against the standard library? Unsure. Ok, I'm filing a bug to the Python developers so

Re: [Tutor] Regex ^$ not behaving as expected

2016-12-08 Thread Danny Yoo
Hi Edmund, For each of the cases that surprise you, next time, can you also say what you expected to see? That can help us see where the confusion lies; as it stands, if we have the same mental model as what's happening in Python, then the results look correct to us. :P I can guess at what you

[Tutor] Regex ^$ not behaving as expected

2016-12-08 Thread Edmund Butterworth via Tutor
Hello, I am new to Python and trying to get to grips with the re regex module. I’m running Python 3.4 under Debian Jessie with a Cinnamon desktop. My understanding is that when the re.M flag is raised, |^| will match at the beginning of the string and also at the beginning of each line withi

Re: [Tutor] Regex/Raw String confusion

2016-08-04 Thread Jim Byrnes
On 08/04/2016 03:27 AM, Alan Gauld via Tutor wrote: On 04/08/16 02:54, Jim Byrnes wrote: Is the second example a special case? phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)') I ask because it produces the same results with or without the ' r '. That's because in this specific

Re: [Tutor] Regex/Raw String confusion

2016-08-04 Thread Alan Gauld via Tutor
On 04/08/16 02:54, Jim Byrnes wrote: > Is the second example a special case? > > phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)') > > I ask because it produces the same results with or without the ' r '. That's because in this specific case there are no conflicts between the regex

Re: [Tutor] Regex/Raw String confusion

2016-08-03 Thread David Rock
> On Aug 3, 2016, at 20:54, Jim Byrnes wrote: > > Is the second example a special case? > > phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)') > mo = phoneNumRegex.search('My phone number is: (415) 555-4242.') > print(mo.group(1)) > print() > print(mo.group(2)) > > I ask because it

Re: [Tutor] Regex/Raw String confusion

2016-08-03 Thread Jim Byrnes
On 08/03/2016 06:21 PM, Alan Gauld via Tutor wrote: On 03/08/16 20:49, Jim Byrnes wrote: Regular Expressions he talks about the python escape character being a '\' and regex using alot of backslashes. In effect there are two levels of escape character, python and the regex processor. Unfortun

Re: [Tutor] Regex/Raw String confusion

2016-08-03 Thread Alan Gauld via Tutor
On 03/08/16 20:49, Jim Byrnes wrote: > Regular Expressions he talks about the python escape character being a > '\' and regex using alot of backslashes. In effect there are two levels of escape character, python and the regex processor. Unfortunately they both use backslash! Python applies its

[Tutor] Regex/Raw String confusion

2016-08-03 Thread Jim Byrnes
I am reading Automate The Boring Stuff With Python. In the chapter on Regular Expressions he talks about the python escape character being a '\' and regex using alot of backslashes. Then he says, However, by putting an r before the first quote of the string value, you can mark the string as a

Re: [Tutor] regex advice

2015-01-06 Thread Cameron Simpson
On 06Jan2015 11:43, Norman Khine wrote: i have the following code: [...] #t(" ") gettext_re = re.compile(r"""[t]\((.*)\)""").findall My first thought is: "[t]" can just be written "t" [...] so, gettext_re = re.compile(r"""[t]\((.*)\)""").findall is not correct as it includes results such

Re: [Tutor] regex advice

2015-01-06 Thread Peter Otten
Norman Khine wrote: > i have a blade template file, as > > replace page > .row > .large-8.columns > form( method="POST", action="/product/saveall/#{style._id}" ) > input( type="hidden" name="_csrf" value=csrf_token ) > h3 #{t("Generate Product for")} #{tt(style.name)}

Re: [Tutor] regex advice

2015-01-06 Thread Steven D'Aprano
On Tue, Jan 06, 2015 at 11:43:01AM +, Norman Khine wrote: > hello, > i have the following code: > > import os > import sys > import re > > walk_dir = ["app", "email", "views"] > #t(" ") > gettext_re = re.compile(r"""[t]\((.*)\)""").findall > > for x in walk_dir: [...] The first step in effe

[Tutor] regex advice

2015-01-06 Thread Norman Khine
hello, i have the following code: import os import sys import re walk_dir = ["app", "email", "views"] #t(" ") gettext_re = re.compile(r"""[t]\((.*)\)""").findall for x in walk_dir: curr_dir = "../node-blade-boiler-template/" + x for root, dirs, files in os.walk(curr_dir, topdown=False):

Re: [Tutor] Regex Question

2013-09-30 Thread Mark Lawrence
On 30/09/2013 21:29, Leena Gupta wrote: Hello, I have a TSV file that has the city,state,country information in this format: Name Display name Code San Jose SJC SJC - SJ (POP), CA (US) San Francisco SFOSFO - SF, CA (US) I need

Re: [Tutor] Regex Question

2013-09-30 Thread Dave Angel
On 30/9/2013 16:29, Leena Gupta wrote: > Hello, > > I have a TSV file that has the city,state,country information in this > format: > Name Display name Code > San Jose SJC SJC - SJ (POP), CA (US) > San Francisco SFOSFO - SF, CA (

[Tutor] Regex Question

2013-09-30 Thread Leena Gupta
Hello, I have a TSV file that has the city,state,country information in this format: Name Display name Code San Jose SJC SJC - SJ (POP), CA (US) San Francisco SFOSFO - SF, CA (US) I need to extract the state and country for each

Re: [Tutor] regex grouping/capturing

2013-06-18 Thread Albert-Jan Roskam
- Original Message - > From: Oscar Benjamin > To: Tutor@python.org > Cc: > Sent: Tuesday, June 18, 2013 12:42 PM > Subject: Re: [Tutor] regex grouping/capturing > > On 18 June 2013 09:27, Albert-Jan Roskam wrote: >>  from pygments.lexer impor

Re: [Tutor] regex grouping/capturing

2013-06-18 Thread Oscar Benjamin
On 18 June 2013 09:27, Albert-Jan Roskam wrote: > from pygments.lexer import RegexLexer, bygroups from pygments.token import * > class IniLexer(RegexLexer): name = 'INI' aliases = ['ini', 'cfg'] filenames = > ['*.ini', '*.cfg'] tokens = { 'root': [ (r'\s+', Text), (r';.*?$', Comment), > (r'\[.*

Re: [Tutor] regex grouping/capturing

2013-06-18 Thread Albert-Jan Roskam
- Original Message - > From: Andreas Perstinger > To: "tutor@python.org" > Cc: > Sent: Friday, June 14, 2013 2:23 PM > Subject: Re: [Tutor] regex grouping/capturing > > On 14.06.2013 10:48, Albert-Jan Roskam wrote: >> I am trying to create a pygmen

Re: [Tutor] regex grouping/capturing

2013-06-14 Thread Andreas Perstinger
On 14.06.2013 10:48, Albert-Jan Roskam wrote: I am trying to create a pygments regex lexer. Well, writing a lexer is a little bit more complex than your original example suggested. > Here's a simplfied example of the 'set' command that I would like to > parse. s = 'set workspace = 6148 he

Re: [Tutor] regex grouping/capturing

2013-06-14 Thread Albert-Jan Roskam
- Original Message - > From: Andreas Perstinger > To: tutor@python.org > Cc: > Sent: Thursday, June 13, 2013 8:09 PM > Subject: Re: [Tutor] regex grouping/capturing > > On 13.06.2013 17:09, Albert-Jan Roskam wrote: >> I have a string of the form "requir

Re: [Tutor] regex grouping/capturing

2013-06-13 Thread Andreas Perstinger
On 13.06.2013 17:09, Albert-Jan Roskam wrote: I have a string of the form "required optional3 optional2 optional1 optional3" ('optional' may be any kind of string, so it's not simply 'optional\d+'. I would like to use a regex so I can distinguish groups. Desired outcome: ('required', 'optional3',

[Tutor] regex grouping/capturing

2013-06-13 Thread Albert-Jan Roskam
  Hello,   I have a string of the form "required optional3 optional2 optional1 optional3" ('optional' may be any kind of string, so it's not simply 'optional\d+'. I would like to use a regex so I can distinguish groups. Desired outcome: ('required', 'optional3', 'optional2', 'optional1', 'optiona

Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-17 Thread Walter Prins
Hi, On 17 April 2013 06:11, Jim Mooney wrote: > > Generally the 2to3 script does an OK job. If you're using Windows it's > > [Python_Dir]\Tools\Scripts\2to3.py. > > > > http://docs.python.org/3/library/2to3 > > > Thanks. I didn't know where to find it and though

Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-17 Thread eryksun
On Wed, Apr 17, 2013 at 1:11 AM, Jim Mooney wrote: 2to3 loops.py > File "", line 1 > 2to3 loops.py >^ > SyntaxError: invalid syntax The script 2to3.py is run from the system's terminal/console shell (e.g. cmd or PowerShell on Windows), not the python shell. If the current dire

Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread Jim Mooney
> Generally the 2to3 script does an OK job. If you're using Windows it's > [Python_Dir]\Tools\Scripts\2to3.py. > > http://docs.python.org/3/library/2to3 Thanks. I didn't know where to find it and thought I had to install it. I opened a command window in my Python3

Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread eryksun
On Tue, Apr 16, 2013 at 9:02 PM, Jim Mooney wrote: > So for online progs, examples, ebooks, etc, that still have Py 2 > examples I can copy, has anyone come up with a nice regex package to > do most of the conversions and produce a mostly Py3 from a Py2? That > might make some things a bit easier.

Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread Michael Weylandt
On Apr 16, 2013, at 21:02, Jim Mooney wrote: > I already tried an example I copied from something online, in Py 2, > that had a ton of print statements. So I did some fast search and > replace to make them Py 3 with (), since I'm using Py 3. (Except Wing > 101 won't do Replace All for some reas

[Tutor] regex to promote Py 2 to Py 3?

2013-04-16 Thread Jim Mooney
I already tried an example I copied from something online, in Py 2, that had a ton of print statements. So I did some fast search and replace to make them Py 3 with (), since I'm using Py 3. (Except Wing 101 won't do Replace All for some reason I haven't figured out, so it's a bit tedious) So for

Re: [Tutor] regex: matching unicode

2012-12-24 Thread eryksun
On Mon, Dec 24, 2012 at 2:51 AM, Albert-Jan Roskam wrote: > > First, check if the first character is a (unicode) letter You can use unicode.isalpha, with a caveat. On a narrow build isalpha fails for supplementary planes. That's about 50% of all alphabetic characters, +/- depending on the version

Re: [Tutor] regex: matching unicode

2012-12-23 Thread Albert-Jan Roskam
>>Is the code below the only/shortest way to match unicode characters? I would >>like to match whatever is defined as a character in the unicode reference >>database. So letters in the broadest sense of the word, but not digits, >>underscore or whitespace. Until just now, I was convinced that th

Re: [Tutor] regex: matching unicode

2012-12-23 Thread eryksun
On Sat, Dec 22, 2012 at 11:12 PM, Steven D'Aprano wrote: > > No. You could install a more Unicode-aware regex engine, and use it instead > of Python's re module, where Unicode support is at best only partial. > > Try this one: > > http://pypi.python.org/pypi/regex Looking over the old docs, I cou

Re: [Tutor] regex: matching unicode

2012-12-22 Thread Steven D'Aprano
On 23/12/12 07:53, Albert-Jan Roskam wrote: Hi, Is the code below the only/shortest way to match unicode characters? No. You could install a more Unicode-aware regex engine, and use it instead of Python's re module, where Unicode support is at best only partial. Try this one: http://pypi.py

Re: [Tutor] regex: matching unicode

2012-12-22 Thread Hugo Arts
On Sat, Dec 22, 2012 at 9:53 PM, Albert-Jan Roskam wrote: > Hi, > > Is the code below the only/shortest way to match unicode characters? I > would like to match whatever is defined as a character in the unicode > reference database. So letters in the broadest sense of the word, but not > digits,

[Tutor] regex: matching unicode

2012-12-22 Thread Albert-Jan Roskam
Hi, Is the code below the only/shortest way to match unicode characters? I would like to match whatever is defined as a character in the unicode reference database. So letters in the broadest sense of the word, but not digits, underscore or whitespace. Until just now, I was convinced that the r

Re: [Tutor] regex question

2012-04-08 Thread kaifeng jin
I think you can do this: a=[] b=redata.split('::') for e in b: if e.find('@') != -1: a.append(e.split('@')[1]) list a includes all the domain 在 2012年4月9日 上午5:26,Wayne Werner 写道: > On Fri, 6 Apr 2012, Khalid Al-Ghamdi wrote: > > hi all, >> I'm trying to extract the domain in the foll

Re: [Tutor] regex question

2012-04-08 Thread Wayne Werner
On Fri, 6 Apr 2012, Khalid Al-Ghamdi wrote: hi all, I'm trying to extract the domain in the following string. Why doesn't my pattern (patt) work: >>> redata 'Tue Jan 14 00:43:21 2020::eax...@gstwyysnbd.gov::1578951801-6-10 Sat Jul 31 15:17:39 1993::rz...@wgxvhx.com::744121059-5-6 Mon Sep 21 2

Re: [Tutor] regex question

2012-04-05 Thread Peter Otten
Khalid Al-Ghamdi wrote: > I'm trying to extract the domain in the following string. Why doesn't my > pattern (patt) work: > redata > 'Tue Jan 14 00:43:21 2020::eax...@gstwyysnbd.gov::1578951801-6-10 Sat Jul > 31 15:17:39 1993::rz...@wgxvhx.com::744121059-5-6 Mon Sep 21 20:22:37 > 1987::ttw..

[Tutor] regex question

2012-04-05 Thread Khalid Al-Ghamdi
hi all, I'm trying to extract the domain in the following string. Why doesn't my pattern (patt) work: >>> redata 'Tue Jan 14 00:43:21 2020::eax...@gstwyysnbd.gov::1578951801-6-10 Sat Jul 31 15:17:39 1993::rz...@wgxvhx.com::744121059-5-6 Mon Sep 21 20:22:37 1987::ttw...@rpybrct.edu::559243357-6-7

Re: [Tutor] regex and parsing through a semi-csv file

2011-10-25 Thread Mina Nozar
Thank you Ramit. I updated my code since I am running 2.7.1+ on Ubuntu. Best wishes, Mina On 11-10-25 08:02 AM, Prasad, Ramit wrote: f = open(args.fname, 'r') lines = f.readlines() f.close() If you are using Python 2.6+ you can use a context manager to automatically close the file. That way

Re: [Tutor] regex and parsing through a semi-csv file

2011-10-25 Thread Prasad, Ramit
>f = open(args.fname, 'r') >lines = f.readlines() >f.close() If you are using Python 2.6+ you can use a context manager to automatically close the file. That way you never have to worry about closing any files! with open(args.fname, 'r') as f: lines = f.readlines() Ramit Ramit Prasad | J

Re: [Tutor] regex and parsing through a semi-csv file

2011-10-24 Thread Mina Nozar
Hi Marc, Thank you. Following some of your suggestion, the rewrite below worked. I agree with your point on readability over complexity. By grace I meant not convoluted or simpler. That's all. As a beginner, I find not knowing all the existing functions, I end up re-inventing the wheel som

Re: [Tutor] regex and parsing through a semi-csv file

2011-10-19 Thread Marc Tompkins
On Wed, Oct 19, 2011 at 12:06 PM, Marc Tompkins wrote: > This approach also works in the event that the data wasn't all collected in > order - i.e. there might be data for Ag111 followed by U235 followed by > Ag111 again. > > Ahem... Of course, I meant "Ag47 followed by U235 followed by Ag47 agai

Re: [Tutor] regex and parsing through a semi-csv file

2011-10-19 Thread Marc Tompkins
On Wed, Oct 5, 2011 at 11:12 AM, Mina Nozar wrote: > Now, I would like to parse through this code and fill out 3 lists: 1) > activity_time, 2) activity, 3) error, and plot the activities as a function > of time using matplotlip. My question specifically is on how to parse > through the lines con

Re: [Tutor] regex and parsing through a semi-csv file

2011-10-19 Thread Mina Nozar
Hello Wayne, Thank you for your help and sorry for the delay in the response. I was caught up with other simulation jobs and didn't get around to testing what you suggested until yesterday. On 11-10-05 01:24 PM, Wayne Werner wrote: On Wed, Oct 5, 2011 at 1:12 PM, Mina Nozar mailto:noz...@tri

Re: [Tutor] regex and parsing through a semi-csv file

2011-10-05 Thread Wayne Werner
On Wed, Oct 5, 2011 at 1:12 PM, Mina Nozar wrote: > > If there is a more graceful way of doing this, please let me know as well. > I am new to python... > I just glanced through your email, but my initial thought would be to just use regex to collect the entire segment that you're looking for

[Tutor] regex and parsing through a semi-csv file

2011-10-05 Thread Mina Nozar
Hi everyone, I am post processing data from the output of simulation of activities for various radionuclide produced in a reaction at different times. I have already combined the information from 13 files (containing calculated activities and errors for 13 different times). The format of thi

Re: [Tutor] regex woes in finding an ip and GET string

2011-06-20 Thread Peter Otten
Gerhardus Geldenhuis wrote: > I am trying to write a small program that will scan my access.conf file > and update iptables to block anyone looking for stuff that they are not > supposed to. > > The code: > #!/usr/bin/python > import sys > import re > > def extractoffendingip(filename): > f =

Re: [Tutor] regex woes in finding an ip and GET string

2011-06-19 Thread Válas Péter
2011/6/19 Gerhardus Geldenhuis > f = open(filename,'r') > filecontents = f.read() > Try f.read().splitlines() instead. > tuples = re.findall(r'^(\d+\.\d+\.\d+\.\d+).*\"GET(.*)HTTP', > filecontents) > This searches the beginning of the lines, but you downloaded the whole page as one strin

Re: [Tutor] regex woes in finding an ip and GET string

2011-06-19 Thread Peter Lavelle
Looking at the regex you have to match an IP address, I think you would need to put a range limit on each of the four octets you are searching for (as each one would be between 1 and 3 digits long.) For example: r = re.match(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b",line) has worked for me.

[Tutor] regex woes in finding an ip and GET string

2011-06-19 Thread Gerhardus Geldenhuis
Hi I am trying to write a small program that will scan my access.conf file and update iptables to block anyone looking for stuff that they are not supposed to. The code: #!/usr/bin/python import sys import re def extractoffendingip(filename): f = open(filename,'r') filecontents = f.read() #19

Re: [Tutor] Regex question

2011-04-03 Thread Peter Otten
Hugo Arts wrote: > 2011/4/3 "Andrés Chandía" : >> >> >> I continue working with RegExp, but I have reached a point for wich I >> can't find documentation, maybe there is no possible way to do it, any >> way I throw the question: >> >> This is my code: >> >> contents = re.sub(r'Á', >> "A", contents

Re: [Tutor] Regex question

2011-04-03 Thread Hugo Arts
2011/4/3 "Andrés Chandía" : > > > I continue working with RegExp, but I have reached a point for wich I can't > find > documentation, maybe there is no possible way to do it, any way I throw the > question: > > This is my code: > >     contents = re.sub(r'Á', > "A", contents) >     contents = re.

Re: [Tutor] Regex question

2011-04-03 Thread Andrés Chandía
I continue working with RegExp, but I have reached a point for wich I can't find documentation, maybe there is no possible way to do it, any way I throw the question: This is my code:     contents = re.sub(r'Á', "A", contents)     contents = re.sub(r'á', "a", contents)     contents = re.sub(r'

Re: [Tutor] Regex question

2011-03-30 Thread Andrés Chandía
Thanks Steve, your are, from now on, my guru this is the final version, the good one! contents = re.sub(r'(|)(l|L|n|N|t|T)(|)', r"\2'" ,contents) On Wed, March 30, 2011 17:27, Steve Willoughby wrote: On 30-Mar-11 08:21, "Andrés Chandía" wrote: > > > Thanks Kushal and Steve. > I think it w

Re: [Tutor] Regex question

2011-03-30 Thread Steve Willoughby
On 30-Mar-11 08:21, "Andrés Chandía" wrote: Thanks Kushal and Steve. I think it works,a I say "I think" because at the results I got a strange character instead of the letter that should appear this is my regexp: contents = re.sub(r'(|)(l|L|n|N|t|T)(|)', '\2\'' ,contents) Remember that \2 i

Re: [Tutor] Regex question

2011-03-30 Thread Andrés Chandía
Thanks Kushal and Steve. I think it works,a I say "I think" because at the results I got a strange character instead of the letter that should appear this is my regexp: contents = re.sub(r'(|)(l|L|n|N|t|T)(|)', '\2\'' ,contents) this is my input file content: lomo  nomo  tomo  Lomo  Nomo 

Re: [Tutor] Regex question

2011-03-30 Thread Steve Willoughby
On 29-Mar-11 23:55, Alan Gauld wrote: ""Andrés Chandía"" wrote in perl there is a way to reference previous registers, $text =~ s/(l|L|n|N)<\/u>/$1e/g; I'm looking for the way to do it in python If you're using just a straight call to re.sub(), it works like this: text = re.sub(r

[Tutor] Regex question

2011-03-30 Thread Alan Gauld
""Andrés Chandía"" wrote I'm new to this list, so hello everybody!. Hi, welcome to the list. Please do not use reply to start a new thread it confuses threaded readers and may mean you message will not be seen. Also please supply a meaningful subject (as above) so we can decide if it looks

Re: [Tutor] regex questions

2011-02-18 Thread Steven D'Aprano
Albert-Jan Roskam wrote: So the raw string \b means means "ASCII backspace". Is that another way of saying that it means 'Word boundary'? No. Python string literals use backslash escapes for special characters, similar to what many other computer languages, including C, do. So when you typ

Re: [Tutor] regex questions

2011-02-18 Thread Albert-Jan Roskam
~~ ________ From: Steven D'Aprano To: Python Mailing List Sent: Fri, February 18, 2011 4:45:42 AM Subject: Re: [Tutor] regex questions Albert-Jan Roskam wrote: > Hello, > > I have a couple of regex questions: > > 1 -- In the code be

Re: [Tutor] regex questions

2011-02-17 Thread Steven D'Aprano
Albert-Jan Roskam wrote: Hello, I have a couple of regex questions: 1 -- In the code below, how can I match the connecting words 'van de' , 'van der', etc. (all quite common in Dutch family names)? You need to step back a little bit and ask, what is this regex supposed to accomplish? What i

[Tutor] regex questions

2011-02-17 Thread Albert-Jan Roskam
Hello, I have a couple of regex questions: 1 -- In the code below, how can I match the connecting words 'van de' , 'van der', etc. (all quite common in Dutch family names)? 2 -- It is quite hard to make a regex for all surnames, but easier to make regexes for the initials and the connecting wor

Re: [Tutor] regex question

2011-01-04 Thread Richard D. Moores
On Tue, Jan 4, 2011 at 14:58, Steven D'Aprano wrote: > Dave Angel wrote: > >> One hazard is if the string the user inputs has any regex special >> characters in it.  If it's anything but letters and digits you probably want >> to escape it before combining it with your \\b strings. > > It is best

Re: [Tutor] regex question

2011-01-04 Thread Steven D'Aprano
Dave Angel wrote: One hazard is if the string the user inputs has any regex special characters in it. If it's anything but letters and digits you probably want to escape it before combining it with your \\b strings. It is best to escape any user-input before passing it to regex regardless.

Re: [Tutor] regex question

2011-01-04 Thread Dave Angel
On 01/-10/-28163 02:59 PM, Richard D. Moores wrote: On Tue, Jan 4, 2011 at 11:57, Richard D. Moores wrote: On Tue, Jan 4, 2011 at 10:41, Richard D. Moores wrote: Please see http://tutoree7.pastebin.com/z9YeSYRw . I'm actually searching RTF files, not TXT files. I want to modify this script t

Re: [Tutor] regex question

2011-01-04 Thread Richard D. Moores
On Tue, Jan 4, 2011 at 11:57, Richard D. Moores wrote: > On Tue, Jan 4, 2011 at 10:41, Richard D. Moores wrote: >> Please see http://tutoree7.pastebin.com/z9YeSYRw . I'm actually >> searching RTF files, not TXT files. >> >> I want to modify this script to handle searching on a word. So what, >> f

Re: [Tutor] regex question

2011-01-04 Thread Richard D. Moores
On Tue, Jan 4, 2011 at 10:41, Richard D. Moores wrote: > Please see http://tutoree7.pastebin.com/z9YeSYRw . I'm actually > searching RTF files, not TXT files. > > I want to modify this script to handle searching on a word. So what, > for example, should line 71 be? OK, I think I've got it. in pl

Re: [Tutor] regex question

2011-01-04 Thread Richard D. Moores
Please see http://tutoree7.pastebin.com/z9YeSYRw . I'm actually searching RTF files, not TXT files. I want to modify this script to handle searching on a word. So what, for example, should line 71 be? Dick ___ Tutor maillist - Tutor@python.org To unsu

Re: [Tutor] regex question

2011-01-04 Thread Brett Ritter
On Tue, Jan 4, 2011 at 11:07 AM, Richard D. Moores wrote: > A file has these 2 lines: > > alksdhjf ksjhdf kjshf dex akjdhf jkdshf jsdhf > alkdshf jkashd flkjdsf index alkdjshf alkdjshf > > And I want the only line that contains the word "dex" Ah! Then you want a slightly different Regex pattern.

Re: [Tutor] regex question

2011-01-04 Thread Richard D. Moores
On Tue, Jan 4, 2011 at 09:31, Brett Ritter wrote: > On Tue, Jan 4, 2011 at 10:37 AM, Richard D. Moores wrote: >> regex = ".*" + search + ".*" >> p = re.compile(regex, re.I) > > Just having "search" as your regex is fine (it will search for the > pattern _in_ the string, no need to specify the ot

Re: [Tutor] regex question

2011-01-04 Thread Brett Ritter
On Tue, Jan 4, 2011 at 10:37 AM, Richard D. Moores wrote: > regex = ".*" + search + ".*" > p = re.compile(regex, re.I) > > in finding lines in a text file that contain search, a string entered > at a prompt. That's an inefficient regex (though the compiler may be smart enough to prune the unneede

Re: [Tutor] regex question

2011-01-04 Thread Richard D. Moores
On Tue, Jan 4, 2011 at 07:55, Wayne Werner wrote: > On Tue, Jan 4, 2011 at 9:37 AM, Richard D. Moores > You could use (2.6+ I think): > word = raw_input('Enter word to search for: ') > with open('somefile.txt') as f: >    for line in f: >        if word in line: >             print line I think

Re: [Tutor] regex question

2011-01-04 Thread Wayne Werner
On Tue, Jan 4, 2011 at 9:37 AM, Richard D. Moores wrote: > I use > > regex = ".*" + search + ".*" > p = re.compile(regex, re.I) > > in finding lines in a text file that contain search, a string entered > at a prompt. > > What regex do I use to find lines in a text file that contain search, > where

[Tutor] regex question

2011-01-04 Thread Richard D. Moores
I use regex = ".*" + search + ".*" p = re.compile(regex, re.I) in finding lines in a text file that contain search, a string entered at a prompt. What regex do I use to find lines in a text file that contain search, where search is a word entered at a prompt? Thanks, Dick Moores __

  1   2   3   >