MySqldump數據導入到Oracle過程中的部分方法簡記

環境 : python 2.7

之前寫過一版 MySql 與 Oracle數據互導的小工具代碼,不過是基於持續連接 兩邊的數據庫的,而且受網速限制比較大,於是直接從備份文件導入到目標數據庫的想法就出現了。

不過由於考慮問題比較大意,此過程中經常對字符串的處理 修修補補。

好在經過N次的測試及使用,暫時對於手頭的數據處理是夠用了。

好了,廢話不多說了,以下是 對MySQL dump出來的文件字符串處理過程中的幾個核心方法

其他的字符串處理 大同小異,有興趣或者需要的 童鞋可以交流探討下


##coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def enEstr(t):#對於MySql 導出數據字段值中的 某些轉義字符  做預處理
    t = t.replace(u'\\\\',u'AJeenA')
    t = t.replace(u"\\'",u'BJeenB')
    t = t.replace(u'\\"',u'CJeenC')
    return t
def deEstr(t):#還原 Mysql 中 轉義字符  爲 Oracle 中 對應可用的
    if get_type(t) in ['str','unicode']:
        t = t.replace(u'AJeenA',u'\\')
        t = t.replace(u'BJeenB',u"'")
        t = t.replace(u'CJeenC',u'"')
    return t
def dlsTostrlist(t):
    temp = [] #括號及單引號配對
    fa = [] #最終返回值
    data = [] # 暫存 fa 中的元素
    for i in xrange(len(t)):
        c = t[i]
        if len(temp) == 0:
            if len(data) > 0:
                fa.append(''.join(data))
                data = []
        elif i == len(t) - 1:
            if c == u')' and len(temp) > 0 and temp[-1] == u'(':
                data.append(c)
                fa.append(''.join(data))
                data = []
        if c == u'(' :
            if len(temp) == 0:
                temp.append(c)
            elif temp[-1] == u'(':
                temp.append(c)
            data.append(c)
            continue
        elif c == u"'" :
            if len(temp) > 0 :
                if temp[-1] == "'":
                    temp.pop()
                else:
                    temp.append(c)
            data.append(c)
            continue
        elif c == u')' :
            if len(temp) > 0 :
                if temp[-1] == u'(':
                    temp.pop()
            data.append(c)
            continue
        elif c == u',' and len(temp) == 0:
            continue
        else:
            data.append(c)
    del temp,data
    return fa
def strTolist(t):
    t = t[1:-1]  #剔除前後的括號
    ds = t.split(u',') # 按 , 進行分割
    fa = [] #存儲返回值
    data = [] #緩存 如'a,bb,ccc'分割後的 過程值 ["'a","bb","ccc"]
    temp = u'' # 用於判斷data 是否已符合拼接要求
    for d in ds:
        if d.startswith(u"'") and d.endswith(u"'") and len(d) > 1: #完整的值 至少爲 '' 空串,排除 '',',abcd' 分割後的異常情況
            fa.append(d[1:-1]) #剔除前後的 單引號
        elif d == u'NULL' : #空值
            fa.append(u'')
        elif d.isdigit() and len(data) == 0: #由數字組成 且不是 形如 'aa,2345,bbcc' 分割後的過程值 '2345'
            fa.append(int(d))
        else: #需要拼接的值 元素
            data.append(d)
            temp = u','.join(data)
            if temp.startswith(u"'") and temp.endswith(u"'") and len(temp) > 1: #符合拼接要求
                fa.append(temp[1:-1]) #剔除前後的 單引號
                data = []
    del temp,data,ds
    return fa
s = u"(1,'2013-12-10 15:06:21','Tcom_id',NULL,'tiBJeenBmCJeenCa)ge','ti,543,m)()aBJeenBg,e_src'),(2,'2013-12-10 15:11:09','Tcom_id','Tp_id','\u963f\u65af\u8482\u82ac','\u963f\u8428\u5fb7\u2019BJeenB')"
'''
此需求,由 MySql dump 生成的文件 導入到 Oracle 的過程中產生
 將形如 s 的 unicode 字符串 轉化成  形如:
    [
        u"(1,'2013-12-10 15:06:21','Tcom_id',NULL,'tiBJeenBmCJeenCa)ge','ti,543,m)()aBJeenBg,e_src')",
        u"(2,'2013-12-10 15:11:09','Tcom_id','Tp_id','\u963f\u65af\u8482\u82ac','\u963f\u8428\u5fb7\u2019BJeenB')"
    ]
    的 unicode 字符串數組
再將 所得到 數組中的字符串 轉化成  形如:
    [1, u'2013-12-10 15:06:21', u'Tcom_id', u'', u'tiBJeenBmCJeenCa)ge', u'ti,543,m)()aBJeenBge_src']
    的數組
#注意#字串中 不能有轉義的 單引號   可在轉化前 進行 enEstr() 處理, 得到結果後 在 deEstr() 進行對應的還原
'''
t = dlsTostrlist(s)
print t
print '\n'
l = t[0]
print l
b = strTolist(l)
print b




真是驗證了一句話“bug只可能被發現,不可能被消滅”

歡迎 指點 或 拍磚


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章