Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85431 > unrolled thread
| Started by | OmPs <torque.india@gmail.com> |
|---|---|
| First post | 2015-02-10 13:00 +0530 |
| Last post | 2015-02-10 13:00 +0530 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
Varable parsing error with python OmPs <torque.india@gmail.com> - 2015-02-10 13:00 +0530
| From | OmPs <torque.india@gmail.com> |
|---|---|
| Date | 2015-02-10 13:00 +0530 |
| Subject | Varable parsing error with python |
| Message-ID | <mailman.18598.1423553413.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
In an XML file which contain a tag and then these tags contain properties,
which later are being called in anoter tag where these properties are
expanded as variables.
For eg.
<instance name='instancename' user='test' group='test'
fullname='test%20-%20dev%20app' >
<property>
<APPVERSION>1.0</APPVERSION>
</property>
</instance>
<component name='testappComp' user='test' group='test'>
<version>1.2</version>
<os>Red Hat Enterprise Linux Server 5 X86_64</os>
<package>testsapppath</package>
</component>
<package name='testapppath'>
<description>my testapp area</description>
<version>${APPVERSION}</version>
</package>
So now i have written a python program whcih is parsing this program, so
what i am doing in here is reading this xml file
with open(filename, "r") as f:
orig_xml = f.read()
#unescape html characters like %20
xmlstring = urllib.unquote(orig_xml)
#convert xml to dict
doc = xmltodict.parse(xmlstring)
#replace property values in xml string via instancename
s = Template(xmlstring)
#iterate over instances/ match the one we need
for i in doc.get("application").get("instance", None):
#when found
# s/r properties in xml and then convert it back to dict
try:
if i.get("@name", None) == instancename:
try:
instance_property = dict(i.get("property"))
final_string = s.safe_substitute(instance_property)
final_dict = xmltodict.parse(final_string)
except TypeError:
final_dict = doc
# Handle strings in case of dict, strings do not have get method.
except AttributeError:
final_dict = doc
and when i am trying to get the version no. using a function for getting
the package version
def _getPackgeVersion(xmlfile, p):
package = str(p)
if isinstance(fpmdict["application"]["package"], list):
for i in fpmdict["application"]["package"]:
if i["@name"] == p:
_pkgVersion = i["version"]
else:
_pkgversion = fpmdict["application"]["package"]["version"]
return _pkgVersion
pkgVersion = _getPackgeVersion(final_doc, testpackage)
print type(pkgVersion)
I am getting the below error
<type 'NoneType'>
Back to top | Article view | comp.lang.python
csiph-web