Re: [Tutor] Using Python and Regex
Thanks for yoru reply. This was my first attempt,when running through idleid get the following error:- Traceback (most recent call last): File "C:\Users\Bill\Desktop\TXT_Output\email_extraction_script.py", line 27, in traverse_dirs(working_dir) File "C:\Users\Bill\Desktop\TXT_Output\email_extraction_script.py", line 20, in traverse_dirs if match: UnboundLocalError: local variable 'match' referenced before assignment My code is as follows: import os import datetime import re now = datetime.datetime.now() timestamp = now.strftime('%d-%m-%Y') output_file = open('OUTPUT - ' + timestamp + '.csv', 'w+') def traverse_dirs(wdir): grabline = 0 for f in os.listdir('.'): if os.path.isfile(f) == True: if "Email Exceeded Maximum Size Limit" in f: continue else: content = open(f) lines = content.readlines() for line in lines: match = re.search(r"\b[^\<][A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}[^\>]\b",l ine) if match: print(match.group(0)) otext = match.group(0) + ",\n" output_file.write(otext) if __name__ == "__main__": working_dir = 'C:\\Users\\Jono\\Desktop\\TXT_Output\\' traverse_dirs(working_dir) -Original Message- From: Tutor [mailto:tutor-bounces+bill5work=outlook@python.org] On Behalf Of Bill Sent: 10 August 2014 12:31 To: tutor@python.org Subject: [Tutor] Using Python and Regex Hi, I'm relatively new to Python and I'm trying to write a script to iterate through a series of text files in folder searching for some specific text and write it to a CSV. I plan to use Regex to match the text and I have already identified the Regex to do this. I've also got as far as creating a CSV using python but, being new to this, I'm looking for some pointers on how to traverse through the folder and open each file in turn to check the content for matches using Regex. Any advice would be gratefully received. Regards Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python twitter "errors":[{"message":"Could not authenticate you", "code":32
On 08/08/2014 09:28 AM, Alan Gauld wrote: > > Example code often uses fictitious security keys. Are you sure these > values are genuine values that work if you use them to access > twitter directly? > Do you mean you used the same keys as in the example? Or have you > tested the keys work via some other method of access? I used my own keys and shortened them for this posting. They were working with an curl example from the twitter's dev homepage. > I can't help there I know nothing about Twitter, far less its API! Now, I've downloaded another twitter-library - and it's working! Nevertheless thank you for your reply. -- Christian ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Python and Regex
Bill wrote: > Thanks for yoru reply. This was my first attempt,when running through > idleid get the following error:- > > > Traceback (most recent call last): > File "C:\Users\Bill\Desktop\TXT_Output\email_extraction_script.py", line > 27, in > traverse_dirs(working_dir) > File "C:\Users\Bill\Desktop\TXT_Output\email_extraction_script.py", line > 20, in traverse_dirs > if match: > UnboundLocalError: local variable 'match' referenced before assignment > > My code is as follows: > for line in lines: > match = > re.search(r"\b[^\<][A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4} [^\>]\b",l > ine) > if match: > print(match.group(0)) > otext = match.group(0) + ",\n" > output_file.write(otext) The indentation of 'if match' is wrong; the way you wrote the line will be executed after the for loop, but you want it inside the loop. You are lucky that the first file you encountered was empty and thus the match variable never set ;) Otherwise the error would have been harder to find. Random remarks: > def traverse_dirs(wdir): > grabline = 0 > for f in os.listdir('.'): The listdir() argument should probably be wdir instead of '.'. > if os.path.isfile(f) == True: The idiomatic way to spell this is if os.path.isfile(f): > content = open(f) > lines = content.readlines() > for line in lines: The readlines() call will put the whole file into a potentially huge list. You don't need to do this for your application. Instead iterate over the file directly: content = open(f) for line in content: That keeps memory consumption low and the data processing can start immediately. PS: The way you wrote it your program will process a single directory. If you want to look into subdirectories you should read up on os.walk() as already suggested. You will end up with something like for path, dirs, files in os.walk(wdir): for name in files: f = os.path.join(path, name) content = open(f) ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Python and Regex
On 11/08/14 08:56, Bill wrote: Just a wee point I noticed: def traverse_dirs(wdir): grabline = 0 for f in os.listdir('.'): if os.path.isfile(f) == True: if "Email Exceeded Maximum Size Limit" in f: continue Note that this checks whether the string is in the file *name*. It does not test if the string is in the file content. Is that really what you want? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Error message
Hi Richard, I would recommend asking the PyGame folks on this one. What you're running into is specific to that external library, and folks who work with PyGame have encountered the problem before: they'll know how to diagnose and correct it. For example: http://stackoverflow.com/questions/7775948/no-matching-architecture-in-universal-wrapper-when-importing-pygame http://stackoverflow.com/questions/8275808/installing-pygame-for-mac-os-x-10-6-8 What appears to be happening is a 32-bit vs 64-bit issue: you may have installed a version of PyGame for one architecture, but should have chosen the other. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] IndentationError
Dear Python Users, Attachment is python code. The function of this code is that I want to insert lots of fiber into matrix, data is a list storing translate vector. When a fiber is translated to matrix, I used fiber to cut matrix. So the new part will be generated, I delete the previous part and resume fiber. The loop will stop when all fibers are inserted to matrix. I used abaqus 6.11 to run fibermatrix.py, but it has problems. Thank you for your help. Best regards, Amy fibermatrix.py Description: Binary data ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndentationError
On 11/08/14 22:28, 王学敏 wrote: for i in range(len(data)): instancename='Part'+str(i)+'-'+str(1) partname='Part'+str(i+1) pre_part='Part'+str(i) mdb.models['Model-1'].rootAssembly.translate(instanceList=('fiber-1', ),vector=(data[i][0], data[i][1], 0.0)) mdb.models['Model-1'].rootAssembly.InstanceFromBooleanCut(cuttingInstances=(... mdb.models['Model-1'].rootAssembly.instances['fiber-1'], ), ... instanceToBeCut=mdb.models['Model-1'].rootAssembly.instances[instancename], ... name=partname, originalInstances=SUPPRESS) del mdb.models['Model-1'].parts['pre_part'] mdb.models['Model-1'].rootAssembly.features['fiber-1'].resume() Like the subject says, you have multiple indentation errors. If you want further help I suggest you tell us what "problems" you get - including full error messages. Also if you can reduce your code to something more readable that will help. Also... from part import * from material import * the import * style of import can lead to problems with name clashes - with the last imported name masking all others. This is doubly true when you have so as many imports as you do, the likelihood of a name being used twice is quite high. Finally, I don't recognise any of the modules you are importing, I assume they are from some specialist library or other. In particular you seem to be referencing something called mdb but that does not appear as a module or variable in your code, so its impossible for us to tell what it is. (Presumably its imported as one of the * names but we have no clue which module it comes from!) You could maybe try asking for help on that library's support forum if the error proves to be library related. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndentationError
On 11/08/2014 22:28, 王学敏 wrote: Dear Python Users, Attachment is python code. The function of this code is that I want to insert lots of fiber into matrix, data is a list storing translate vector. When a fiber is translated to matrix, I used fiber to cut matrix. So the new part will be generated, I delete the previous part and resume fiber. The loop will stop when all fibers are inserted to matrix. I used abaqus 6.11 to run fibermatrix.py, but it has problems. Thank you for your help. Best regards, Amy I'm awfully sorry but I'm not downloading a 22kb file just to find an IndentationError. Please see this http://sscce.org/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor