I want to validate a 2- or 3-letter iso code, but also allow it to be empty (so it can be 0, 2, or 3 characters).
'\w{2,3}|'
This works locally and also on http://www.freeformatter.com/xml-validator-xsd.html. However, when I try running it on an ubuntu machine, I get the following error:
>>> import urllib2
>>> from lxml import etree
>>> xsd_url = 'https://s3-us-west-1.amazonaws.com/premiere-avails/movie.xsd.xml'
>>> xsd_contents = urllib2.urlopen(xsd_url).read()
>>> xmlschema_doc = etree.fromstring(xsd_contents)
>>> xmlschema=etree.XMLSchema(xmlschema_doc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "xmlschema.pxi", line 102, in lxml.etree.XMLSchema.__init__ (src/lxml/lxml.etree.c:168126)
lxml.etree.XMLSchemaParseError: Element '{http://www.w3.org/2001/XMLSchema}pattern':
The value '\w{2,3}|' of the facet 'pattern' is not a valid regular expression., line 58
What would be a better regex pattern for this? (\w{2,3})? also fails with xsd, so it needs to be something else.
Related
I have a file that gets generated by :
excerpt:
group0 = ['ParentPom']
group1 = ['Commons','http', 'availability','ingestPom','abcCommons','solrIndex','123Service']
...
group10=['totalCommons','Generator']
How can I include this in my python script, tried import but no luck
>>> import dependencies_custom
>>> print (group2[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'group2' is not defined
In the import form you're using, you should be able to access the groups by
dependencies_custom.group2[0]
type notation. If you want just use just group2[0] notation, try using:
from dependencies_custom import *
I am trying to convert string date to timestamp in Python as described in the post here. When I run the code examples in the post, I encounter an error. For e.g.:
>>> import time
>>> import datetime
>>> s = "01/12/2011"
>>> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
File "C:\Python34\lib\_strptime.py", line 15, in <module>
import calendar
File "C:\Python34\calendar.py", line 9, in <module>
calendar = wckCalendar.Calendar(root, command=echo)
NameError: name 'wckCalendar' is not defined
>>>
It looks like at runtime the code implicitly tries to import calendar and throws the error in the process. When I import calendar directly, I get the same error:
>>> import calendar
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
import calendar
File "C:\Python34\calendar.py", line 9, in <module>
calendar = wckCalendar.Calendar(root, command=echo)
NameError: name 'wckCalendar' is not defined
>>>
I just want to be able to do the following:
>>> import time
>>> import datetime
>>> s = "01/12/2011"
>>> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
Any ideas?
Python 3.4.1 on Windows 7 32 bit*
Thanks to #J.F. Sebastian. I was caught in the name shadowing trap. I made the stupid mistake of naming one of my modules calendar.py. Once I removed this file, everything ran fine. I hope this post is not a waste and that someone may someday find it useful.
I'm a beginner using python. I want to create a regular expression to capture error messages from compiler output in python. How would I do this?
for example, if the compiler output is the following error message:
Traceback (most recent call last):
File "sample.py", line 1, in <module>
hello
NameError: name 'hello' is not defined
I want to be able to only extract only the following string from the output:
NameError: name 'hello' is not defined
In this case there is only one error, however I want to extract all the errors the compiler outputs. How do I do this using regular expressions? Or if there is an easier way, I'm open to suggestions
r'Traceback \(most recent call last\):\n(?:[ ]+.*\n)*(\w+: .*)'
should extract your exception; a traceback contains lines that all start with whitespace except for the exception line.
The above matches the literal text of the traceback first line, 0 or more lines that start with at least one space, and then captures the line following that provided it starts with 1 or more word characters (which fits Python identifiers nicely), a colon, and then the rest up to the end of a line.
Demo:
>>> import re
>>> sample = '''\
... Traceback (most recent call last):
... File "sample.py", line 1, in <module>
... hello
... NameError: name 'hello' is not defined
... '''
>>> re.search(r'Traceback \(most recent call last\):\n(?:[ ]+.*\n)*(\w+: .*)', sample).groups()
("NameError: name 'hello' is not defined",)
Why does the following result in an error?
import re
from urllib import quote as q
s = re.compile(r'[^a-zA-Z0-9.: ^*$#!+_?-]')
s.sub(q, "A/B")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/python/python-2.7.1/lib/python2.7/urllib.py", line 1236, in quote
if not s.rstrip(safe):
AttributeError: rstrip
I'd like to call sub on strings that contain forward slashes, not sure why it results in this error. How can it be fixed so that I can pass strings with '/' characters in them to sub()?
thanks.
Because re.sub calls the repl parameter with an instance of re.match.
I think you want to use:
s.sub(lambda m: q(m.group()), "A/B")
However, a simpler way of doing this might be to use the safe argument to urllib.quote:
urllib.quote("A/B", safe="/.: ^*$#!+_?-")
I get a HeaderParseError if I try to parse this string with decode_header() in python 2.6.5 (and 2.7). Here the repr() of the string:
'=?iso-8859-1?B?QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw==?='
This string comes from a mime email which contains a JPEG picture. Thunderbird can
decode the filename (which contains German umlauts).
>>> from email.header import decode_header
>>> decode_header('=?iso-8859-1?B?QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw==?=')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/email/header.py", line 101, in decode_header
raise HeaderParseError
email.errors.HeaderParseError
It seems an incompatibility between Python's character set for base64-encoded strings and the mail agent's:
>>> from email.header import decode_header
>>> a='QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw=='
>>> decode_header(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/email/header.py", line 108, in decode_header
raise HeaderParseError
email.errors.HeaderParseError
>>> a1= a.replace('_', '/')
>>> decode_header(a1)
[('Anmeldung Netzanschluss S\xecdring3p.jpg', 'iso-8859-1')]
>>> print _[0][0].decode(_[0][1])
Anmeldung Netzanschluss Südring3p.jpg
Python utilizes the character set that the Wikipedia article suggests (i.e 0-9, A-Z, a-z, +, /). In that same article, some alternatives (including the underscore that's the issue here) are included; however, the underscore's value is vague (it's value 62 or 63, depending on the alternative).
I don't know what Python can do to guess the intentions of b0rken mail agents; so I suggest you do some appropriate guessing whenever decode_header fails.
I'm calling “broken” the mail agent because there is no need to escape either + or / in a message header: it's not a URL, so why not use the typical character set?