Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532853
  • 博文数量: 137
  • 博客积分: 3170
  • 博客等级: 中校
  • 技术积分: 1455
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-17 11:47
文章分类

全部博文(137)

文章存档

2015年(2)

2013年(1)

2012年(6)

2011年(5)

2010年(62)

2009年(61)

我的朋友

分类: Python/Ruby

2010-04-19 14:36:24

1.print 用法,

strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
print strHello

c/c++中 printf用法

printf("%c-%c-%c-%c\n",'D','e','m','o');

2.

from xml.dom import minidom
xmldoc = minidom.parse('config.xml')
x=xmldoc.getElementsByTagName("body")[0].childNodes[0];
x.nodeValue="Hello World";
print xmldoc.toxml()
f=file('config.xml', 'w')
import codecs
writer = codecs.lookup('utf-8')[3](f)
xmldoc.writexml(writer, encoding='utf-8')
writer.close()
##################

    Tove
    Hello World
    Reminder
    Hello World

3.python 合并sql文件,并按数据库分类存放于不同文件


#!/usr/bin/python
##########jacky####################
##### merge sql files according time
import os ,sys, re , shutil

#count args check
if len(sys.argv) != 3 :
    print 'arg count error'
    exit()

orginSqldir = sys.argv[1] #the orgin sql files dir
beginfname = sys.argv[2] #the first file to merge
beginfdate = int( beginfname[:8] ) #the begin time to merge
# which db does l belongs to
def whichdb( l ):
    if l.find('tst1') != -1 :
        return 0
    elif l.find('tst2') != -1 :
        return 1
    elif l.find('tst3') != -1 :
        return 2
    elif l.find('tst4') != -1 :
        return 3
    elif l.find('tst5') != -1 :
        return 4
    else :
        return -1
#merge sql file content to each dbfile
def cpsql2dbfile( sqlfsrc, fh_dst ):
    abs_src_sqlf = os.path.join(orginSqldir, sqlfsrc)
    fh_src = open(abs_src_sqlf, 'r')
    contnt_file = fh_src.readlines()
    fh_dst.writelines(contnt_file)
    fh_dst.write('\n')
    fh_src.close()

#store all the meet time sql files
dic_all = {}
dic_all['tst1'] = []
dic_all['tst2'] = []
dic_all['
tst3'] = []
dic_all['
tst4'] = []
dic_all['
tst5'] = []

for file in os.listdir(orginSqldir):
    filepath = os.path.join( orginSqldir,file)
    if os.path.isfile(filepath):
        tmp = file[:8]
        if tmp.isdigit():
            if int(tmp) >= beginfdate:
                if whichdb( file ) == 0 :
                    dic_all['tst1'].append( file )
                elif whichdb( file ) == 1:
                    dic_all['
tst2'].append( file )
                elif whichdb( file ) == 2:
                    dic_all['
tst3'].append( file )
                elif whichdb( file ) == 3:
                    dic_all['
tst4'].append( file )
                elif whichdb( file ) == 4:
                    dic_all['
tst5'].append( file )
                else :
                    print 'error'
                    exit()

#store the merged sql files in new dir
from datetime import date
sqlSortDirDst = date.today().strftime("%Y%m%d") + "VerSubmitSqls"
sqlSortDirDstPath = os.path.join( orginSqldir,sqlSortDirDst )
#
if os.path.exists( sqlSortDirDstPath ):
     shutil.rmtree( sqlSortDirDstPath )
os.mkdir( sqlSortDirDstPath )

#walk through dic_all
for k, v in dic_all.iteritems():
    if len( v ) == 0:
        continue
    k_sql = k + '.sql' ##suffix
    abs_dst_db_fpath = os.path.join(sqlSortDirDstPath , k_sql )
#    print abs_dst_db_fpath
    fh_dst = open( abs_dst_db_fpath, 'w')
    v.sort()
    for iv in v:
         cpsql2dbfile( iv, fh_dst )
    fh_dst.flush()
    fh_dst.close()


阅读(854) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~