Created
December 21, 2010 09:40
-
-
Save alice1017/749718 to your computer and use it in GitHub Desktop.
辞書をXMLにします
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#coding:utf-8 | |
import funcs | |
import types | |
import sys | |
import os | |
class MakeXML(): | |
"""このモジュールは自分で項目をつくりながらXMLファイルを生成できるものです | |
まず「タグにしたい文字列」をキーに、「タグの間にいれる文字列」を要素にいれた辞書をユーザがつくり、それをXMLタグ化します。 | |
タグをネストしたい場合は、ツリーの一番上にくるタグを辞書のキーにして、要素を複数の辞書をいれたリストに追加すれば可能 | |
ただし、現段階では辞書のネストは1段階までしか不可能。 | |
""" | |
def __init__(self,dictionary): | |
self.dict = dictionary | |
self.line = "" | |
for i,j in self.dict.iteritems(): | |
self.key = i | |
self.value = j | |
# proccess 1 : totag | |
t = funcs.toTag(self.key) | |
t.closeit() | |
self.Open_tag = t.tag | |
self.Close_tag = t.close_tag | |
del t | |
# proccess 2 : values to tag | |
if ( isinstance(self.value,types.DictType) == True ): | |
self.line = str(self.Open_tag)+"\n" | |
for i,j in self.value.iteritems(): | |
key = str(i) | |
value = str(j) | |
t = funcs.toTag(key) | |
t.closeit() | |
self.Next_open_tag = t.tag | |
self.Next_close_tag = t.close_tag | |
self.tags = "\t"+str(self.Next_open_tag)+str(value)+str(self.Next_close_tag) | |
self.line += str(self.tags)+"\n" | |
del t | |
self.line += str(self.Close_tag)+"\n" | |
elif ( isinstance(self.value,types.StringType) == True ): | |
self.tags = str(self.Open_tag)+str(self.value)+str(self.Close_tag) | |
self.line += str(self.tags)+"\n" | |
print self.line | |
def help(): | |
print __doc__ | |
def save(self): | |
"""書いたXMLタグを保存します | |
""" | |
f = open(".save.txt","a+") | |
f.write(self.line) | |
f.close() | |
def call(self): | |
"""保存していたタグを呼び出します | |
""" | |
f = open(".save.txt","r") | |
list = [] | |
for i in f: | |
list.append(i) | |
f.close() | |
self.calltag = ''.join(list) | |
print self.calltag | |
def filein(self,filename,version=None,encoding=None): | |
"""作ったXMLタグをファイルに書き込みます | |
メソッドの引数にXMLのversion,encodingを書きこむと、 | |
XMLタグに付加させます。 | |
引数に作りたいファイルの名前をかくだけで良いです | |
事前にsavetagを呼んでいる場合はその中身も取り出して出力します | |
""" | |
if ( os.access("save.txt",os.F_OK) == True ): | |
f = open("save.txt","r") | |
list = [] | |
for i in f: | |
list.append(i) | |
f.close() | |
tags = ''.join(list) | |
if ( (version != None) and (encoding != None) ): | |
string="<?xml verison="+str(version)+" encoding="+str(encoding)+" ?>\n" | |
self.f = open(filename,"a+") | |
self.f.write(string+tags+self.line) | |
self.f.close() | |
elif ( (version != None) and (encoding == None) ): | |
string="<?xml verison="+str(version)+" ?>\n" | |
self.f = open(filename,"a+") | |
self.f.write(string+tags+self.line) | |
self.f.close() | |
elif ( (version == None) and (encoding != None) ): | |
string="<?xml encoding="+str(encoding)+" ?>\n" | |
self.f = open(filename,"a+") | |
self.f.write(string+tags+self.line) | |
self.f.close() | |
else: | |
string = "<?xml ?>" | |
self.f = open(filename,"a+") | |
self.f.write(string+tags+self.line) | |
self.f.close() | |
os.system("rm save.txt") | |
else: | |
self.f = open(filename,"a+") | |
self.f.write(self.line) | |
self.f.close() | |
def quit(self): | |
funcs.check() | |
del self.line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#coding:utf-8 | |
import os | |
class toTag(): | |
def __init__(self,string): | |
self.string = string | |
self.tag = "<"+self.string+">" | |
def closeit(self): | |
self.close_tag = "</"+self.string+">" | |
def check(): | |
if ( os.access(".save.txt",os.F_OK) == True ): | |
os.system("rm save.txt") | |
else: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment