0
計劃1:基於功能: -實現類概念並不在Python工作
import xml.etree.ElementTree as ET
import sys
doc = ET.parse("users.xml")
root = doc.getroot()
root_new = ET.Element("users")
for child in root:
username = child.attrib['username']
password = child.attrib['password']
# create "user" here
user = ET.SubElement(root_new, "user")
user.set("username",username)
user.set("password",password)
#checking attribute for skipping KeyError
if 'remote_access' in child.attrib:
remote_access = child.attrib['remote_access']
user.set("remote_access",remote_access)
for g in child.findall("group"):
# create "group" here
group = ET.SubElement(user,"group")
if g.text != "lion":
group.text = g.text
tree = ET.ElementTree(root_new)
tree.write(sys.stdout)
這是XML: -
<users>
<user username="admin" password="admin" remote_access="yes"></user>
<user username="private_user1" password="user1" ><group>group1</group><group>group2</group></user>
<user username="private_user2" fullname="user2" password="user2"><group>group1</group><group>group2</group></user>
</users>
我的課implementaion: - 完全錯誤的:(
import xml.etree.ElementTree as ET
import sys
class users_detail (object):
def __init__(self, xml_path):
"""bla bla
"""
try:
doc = ET.parse("users.xml")
except:
print 'xml not found'
root = doc.getroot()
root_new = ET.Element("users")
for child in root:
username = child.attrib['username']
password = child.attrib['password']
user = ET.SubElement(root_new, "user")
user.set("username",username)
user.set("password",password)
if 'remote_access' in child.attrib:
remote_access = child.attrib['remote_access']
for g in child.findall("group"):
group = ET.SubElement(user,"group")
if g.text != "lion":
group.text = g.text
tree = ET.ElementTree(root_new)
tree.write(sys.stdout)
if __name__=='main':
users_detail()
如何通過面向對象的概念類以更好的方式實現這個,而我的類實現根本不起作用。阿斯幫助我。 :(
我需要上面的代碼轉換爲Python類:多數民衆贊成在需求量的:(
你似乎對類沒有太多瞭解,我建議你beagin閱讀關於這個主題的書http://www.itmaybeahack.com/homepage/books/python.html,即使有人會im爲你打氣,在你打開基礎之前,對你來說沒什麼用處。目前這個課程完全沒有用(即使它會起作用) – root
這個人是誰?誰將實現:(任何如何感謝書 –
順便說一句,你的「功能實現」不在「功能實現」附近的任何地方 – jsbueno