wiki整理 Python脚本

要整理wiki结构。额,让我做这个。好多表。

首先在

show create table table_name;

CREATE TABLE `general_log` (
  `event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '测试',
  `user_host` mediumtext NOT NULL COMMENT '测试',
  `thread_id` bigint(21) unsigned NOT NULL COMMENT '测试',
  `server_id` int(10) unsigned NOT NULL COMMENT '测试',
  `command_type` varchar(64) NOT NULL COMMENT '测试',
  `argument` mediumtext NOT NULL COMMENT '测试'
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log'

然后复制在wiki中加入在下面的标签里面。

<pre class="sql">
CREATE TABLE `general_log` (
  `event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '测试',
  `user_host` mediumtext NOT NULL COMMENT '测试',
  `thread_id` bigint(21) unsigned NOT NULL COMMENT '测试',
  `server_id` int(10) unsigned NOT NULL COMMENT '测试',
  `command_type` varchar(64) NOT NULL COMMENT '测试',
  `argument` mediumtext NOT NULL COMMENT '测试'
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log'
</pre>

然后用脚本生产表结构,要是手写就要命了

#coding=utf-8
        
def createWiki(str,out):

    items =str.split()
    
    length=len(items)
    #约束的位置
    index_constraints=0
    #注释的位置
    index_comment=0
    

    for i in range(1,length):
        #print items[i]
        if(items[i]=="unsigned"):
            index_constraints=i
            
            continue;
        if(index_constraints==0):
            if(items[i]=="NOT"):
                index_constraints=i
                
                continue;
        if(index_constraints==0):
            if(items[i]=="NULL"):
                index_constraints=i
                
                continue;
        if(index_constraints==0):
            if(items[i]=="DEFAULT"):
                index_constraints=i
                
                continue;
        #找到注释的位置
        if(items[i]=="COMMENT"):
            index_comment=i
            break;
        
    
        
    out.write("|--
")    
    #添加列名
    out.write("|"+items[0]+"
")
    #添加列的类型
    out.write("|"+items[1]+"
")
    
    #添加约束
    if(index_constraints!=0):
        out.write("|")    
        if(index_comment!=0):
        
            for k in range(index_constraints,index_comment):
                out.write(items[k]+" ")
            out.write("
")
        else:
            for j in range(index_constraints,length):
                out.write(items[j]+" ")
            out.write("
")
            
        
    
    else:
        out.write("|
")
    
    #out.write("|"+items[i+1]+"
".decode('gbk'))
    #添加注释
    if(index_comment!=0):
        out.write("|")
        if(index_comment==length):
            out.write(items[index_comment+1]+"
")
        else:
        
            for k in range(index_comment+1,length):
                out.write(items[k]+" ")
            out.write("
")
        
    else:
        out.write("|
")

def openFiles(out):
    file=open("data.txt")
    #增加4列分别是|字段
"+"|类型
"+"|约束
"+"|注释
    str="{|style="color:white;background-color:rgb(18,30,49); font-weight:bold;" cellpadding="10" cellspacing="0" border="1""+"
"+"|-
"+"|字段
"+"|类型
"+"|约束
"+"|注释
";
    out.write(str)
    lines = file.readlines()
    file.close()
    length=len(lines)
    #print length
    #第一行和最后一行不用分析
    for i in range (1,length-1):
        #去掉回车和开头的空格
        line=lines[i].strip('
').lstrip( )
        #print `i` +" : "+ line 
        #如果开头是`表示是一列,为了排除这样列 KEY `idx_pid` (`pid`),
 
        if(line[0]=="`"):
            #print `i` +" : "+ line  
            createWiki(line,out)    
        
if __name__=="__main__" :
    out=open('data1.txt','w')
    openFiles(out)
    out.write("|}")
    out.close()
    file=open("data1.txt")
    lines = file.readlines()
    for line in lines:
        print line
    

然后复制到wiki里。

原文地址:https://www.cnblogs.com/aosting/p/3456206.html