[Tutor] UnicodeEncodeError
I'm doing a simple excercise in reading a file, and printing each line. However, I'm getting this error. The file is a windows txt file, ecoded in ANSI(ascii). I don't understand why Pythin is displaying a Unicode error. Here is my script: f=open('I:\\PythonScripts\\statement.txt') for line in f: print (line) f.close() statement.txt is just a copy of the text from an article at (http://www.tgdaily.com/content/view/43296/98/) into notepad. The error I get is: Traceback (most recent call last): File "I:\PythonScripts\fileloop.py", line 9, in print (line) File "C:\Python31\lib\encodings\cp437.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u2014' in position 10: character maps to I've looked this up, and see that others have had a similar error; however, I don't see any thing saying what I should be encoding to/from since my input and output files are both ascii. -- View this message in context: http://www.nabble.com/UnicodeEncodeError-tp24554280p24554280.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Issues with regex escaping on \{
My regex is being run in both Python v2.6 and v3.1 For this example, I'll give one line. This lines will be read out of log files. I'm trying to get the GUID for the User ID to query a database with it, so I'd like a sub match. Here is the code - import re line = '>Checking Privilege for UserId: {88F96ED2-D471-DE11-95B6-0050569E7C88}, PrivilegeId: {71AD2527-8494-4654-968D-FE61E9A6A9DF}. Returned hr = 0' pUserID=re.compile('UserID: \{(.+)\}',re.I) #Sub match is one or more characters between the first set of squigglies immediately following 'UserID: ' #the output is: (re.search(pUserID,line)).group(1) '88F96ED2-D471-DE11-95B6-0050569E7C88}, PrivilegeId: {71AD2527-8494-4654-968D-FE61E9A6A9DF' --- Why isn't the match terminating after it finds the first \} ? -- View this message in context: http://www.nabble.com/Issues-with-regex-escaping-on-%5C%7B-tp24724060p24724060.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Issues with regex escaping on \{
Thanks! The ? got rid of the greediness! vince spicer wrote: > > On Wed, Jul 29, 2009 at 11:35 AM, gpo wrote: > > > your grouping (.+) appears to be greedy, you can make it non-greedy with a > question mark > > EX: > > pUserID=re.compile('UserID:\s+{(.+?)}',re.I) > > Vince > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- View this message in context: http://www.nabble.com/Issues-with-regex-escaping-on-%5C%7B-tp24724060p24726284.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] New Issues with REGEX Greediness:
Thanks for the replies in the last thread. I'm still trying to figure out how Python is handling RegEx. I'm converting my thoughts from Perl (the mother of regex) to Python, and expect the same results. Why are they different? Perl: $line='>Checking Privilege for UserId: {874BE70A-194B-DE11-BE5C-000C297901A5}, PrivilegeId: {prvReadSdkMessage}. Returned hr = 0' $line=~/(\w+)\:.+.{8}-.{4}-.{4}-.{4}-.{12}/; print "$1" RESULT: UserId This is the result I want. I'm trying to get the name's of GUID's from log files. Python: line='>Checking Privilege for UserId: {874BE70A-194B-DE11-BE5C-000C297901A5}, PrivilegeId: {prvReadSdkMessage}. Returned hr = 0' (re.search('(\w+)\:.+.{8}-.{4}-.{4}-.{4}-.{12}',line)).group(0) RESULT 'UserId: {874BE70A-194B-DE11-BE5C-000C297901A5' How/Why are these results different? What is Python doing differently in regex, that I need to adjust to? -- View this message in context: http://www.nabble.com/New-Issues-with-REGEX-Greediness%3A-tp24764404p24764404.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor