Archive for category python
XML with Python : ตอนที่ 1 อ่าน XML เทียบกับ Groovy
Posted by boysbee in groovy, programming, python on 22/04/2011
วิธีอ่านด้วย Java ลองดูได้ที่ post เก่า ที่นี่นะครับ
วิธีอ่านด้วย Groovy
package com.my.test.xml
def xml = """
<author>
<name>boysbee (boysbee)</name>
<uri>http://twitter.com/boysbee</uri>
</author>
"""
def author = new XmlParser().parseText(xml)
author.each{
println "tag name : " + it.name()+ " = " +it.text()
}
ลองดู Python บ้าง
from xml.dom import minidom xml = "<author><name>boysbee (boysbee)</name><uri>http://twitter.com/boysbee</uri></author>" xmldoc = minidom.parseString(xml) print xmldoc.toxml()
สั้นและง่ายพอ ๆ กันเลย … ^ ^”
XML with Python : ตอนที่ 2 แล้วสร้างเอกสาร XML เทียบกับ Groovy ดูละกัน
Posted by boysbee in groovy, programming, python on 22/04/2011
ดู code java ที่ post เก่าได้นะครับจะได้เปรียบเทียบกันได้ ที่นี่
วิธีสร้างด้วย Groovy
package com.my.xml
def sw = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(sw)
xml.author(){
name("boysbee (boysbee)")
uri("http://twitter.com/boysbee")
}
print sw
สร้างด้วย python
from xml.dom.minidom import Document
doc = Document()
author = doc.createElement("author")
name = doc.createElement("name")
uri = doc.createElement("uri")
nameText = doc.createTextNode("boysbee (boysbee)")
name.appendChild(nameText)
uriText = doc.createTextNode("http://twitter/boysbee")
uri.appendChild(uriText)
author.appendChild(name)
author.appendChild(uri)
doc.appendChild(author)
print doc.toprettyxml(indent=" ")
python ยาวกว่านิดหน่อย ^ ^”
Install Python
Posted by boysbee in programming, python on 19/04/2011
Install
download ตัวติดตั้งจากที่นี่ก่อน download python พอได้ตัว install มาเปิดมาเลือกไดเรคทอรีที่จะวางเลือก ‘Next’ ไปจน ‘Finish’ ก็เรียบร้อยแล้วครับ
Set Environment
ทดสอบ
ไปที่ start > run > cmd แล้วลองพิมพ์ python –v ดูครับ