2012. 1. 16.

extract list from XML (xmldom) in python


data ="
 aaa
 bbb
 ccc
 ddd
 eee
"


from xml.dom.minidom import parse, parseString

#data is xml formatted string 

dom3 = parseString( data )   #make dom object
tagList = dom3.getElementsByTagName("tag1")  #select list "tag1"

txt = tagList[0].firstChild.nodeValue  #first node->txtNode->nodeValue
print txt

===result==========
aaa

How to clear console in python


# # Screen clear 
# You can use it when you make  a console type.

import os
if os.name == "posix":
   os.system('clear')   # not MS-series
elif os.name in ("nt", "dos", "ce"):
   os.system('CLS')   #  MS-series --> DOS/Windows

2012. 1. 13.

python formating datetime


from datetime import datetime
dt = datetime.now()
print dt.strftime("%Y.%m.%d %H.%M.%S")

#  output #
2012.01.13 00.03.01

2012. 1. 8.

python decode string.

json.loads(data)  #when you meet some charset errors

json.loads(data.decode("utf-8"))

2011. 12. 14.

insert file list into sqlite-db table by python


~                                                                                                        import os,sqlite3

con = sqlite3.connect("sqlite_test.db")
cur = con.cursor()

flist = os.listdir("/Users/path/")
for ff in flist:
        f = (ff,)
        cur.execute('insert into tbl_local (vcpath) values(?)' , f )  # watch out, question-mark.  there is no quotation mark
con.commit()
con.close()