Skynet

          ---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ----------

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            112 Posts :: 1 Stories :: 49 Comments :: 0 Trackbacks

          實際問題
          主要在編碼問題下。
          我把這解決的辦法說下吧,主要這統一 編碼 utf8
          0. 數據庫 導入 utf8
             如果 導入文本 gbk
                perl -MEncode -ne 'print encode("UTF-8", decode("GBK",$_));'  file.txt > xx.data
          1. 代碼編碼 # -*- coding:UTF8 -*-
          2. 文件讀取 編碼 cc=bm(x.readline()).split('"t')
                bm=lambda str : str.decode('gbk').encode('utf-8')
          3. 對外輸出 str="%s%s"t" % (str,bmug(f))
                bmug=lambda str : unicode(str).encode("utf8")

          就是統一編碼 ,我這統一 utf8
            
          # -*- coding: UTF8 -*-
          import sqlite3,os
          """
          drop table imusic ;
          create table  imusic(    
            musicname   varchar(300) ,
            productid    varchar(300) ,
            downcount   varchar(300) ,
            musicname2   varchar(300) ,
            actorname2 varchar(300) ,
             iszl varchar(300) ,
             isqu varchar(300) ,
             zlmmf  varchar(300) ,
             zlmp3 varchar(300) ,
             qump3 varchar(300) ,
             cpid varchar(300) ,
             cpname varchar(300) ,
             rows varchar(300) ,
             tmp1 varchar(300) 
          );

          如果要使用主鍵
          在建表第一列加  id integer auto_increment  PRIMARY KEY
          文本就要先處理下來
          awk 'BEGIN{vi=0}{vi++;print vi"\t"$0;}' 03.data > 03.id.data


          # 文本 "t 分割  導入
          .separator "\t"
          .import db2.data imusic
          .separator ","

          #導出
          sqlite> .output a.txt
          sqlite> select * from tab_xx;
          sqlite> .output stdout


          """

          conn 
          = sqlite3.connect("ex1")
          cur 
          = conn.cursor()

          #編碼轉換 (我這 在 windows 下)
          bm=lambda str : str.decode('gbk').encode('utf-8')
          bmcc
          =lambda str : str.decode('utf8').encode('gbk')
          bmug
          =lambda str : unicode(str).encode("utf8")

          def  getValues(actor,music,zlxz,qqxz):
               
          try:
                   sql
          =" select * from imusic  where musicname2=?  and actorname2=?   "  
                   cur.execute(sql,( music,actor)  ) 
                   res 
          = cur.fetchall()
                   
          if res :
                        data
          =[]
                        
          for tt in res[0]:
                           data.append(tt)
                        data[
          7]=''
                        
          print zlxz,"*",qqxz
                        
          if data[8]=='' and  data[9]=='' :
                            
          return None
                        
          if  zlxz=='有振鈴下載'  :
                            data[
          8]=''
                        
          if  qqxz=='有全曲下載'  :
                            data[
          9]=''
                        
          if data[8]=='' and  data[9]=='' :
                            
          return None
                        
          return data
               
          except Exception, myError:
                    
          print "[error] %s %s %s %s" % (actor,music,zlxz,qqxz)
                    
          = open( "all_comm.txt""r")
          cc
          =x.readline().split('\t')
          while cc :
              
          try:
                  cc
          =bm(x.readline()).split('\t')
                  dd
          =getValues(cc[6],cc[5],cc[21],cc[18])
                  
          if dd :
                      str 
          = "" 
                      
          for f in dd :
                          str
          ="%s%s\t" % (str,bmug(f))
                      
          print str
              
          except Exception, myError:
                    
          print "[error] %s" %   cc


          下面 demo 注重 update 對比
              # conn.total_changes 使用
          為 update 跟新
          # -*- coding: UTF8 -*-
          import sqlite3,os,sys
          conn 
          = sqlite3.connect("ex4")
          cur 
          = conn.cursor()

          #編碼轉換 (我這 在 windows 下)
          bm=lambda str : str.decode('gbk').encode('utf-8')
          bmcc
          =lambda str : str.decode('utf8').encode('gbk')
          bmug
          =lambda str : unicode(str).encode("utf8")



          def  getValues(actor,music,zlxz,qqxz,updateRow):
               
          try:
                   updateSql 
          = " update  imusic set tmp1=? where musicname2=?  and actorname2=?  "
                   
          # 1 有振鈴 有全曲
                   # 2 無振鈴 有全曲
                   # 3 有振鈴 無全曲
                   # 4 無振鈴 無全曲
                   start = "0" ;
                   
          if  zlxz=='有振鈴下載' and  qqxz=='有全曲下載'  :
                        start
          ="1"
                   
          if  zlxz=='無振鈴下載' and  qqxz=='有全曲下載'  :
                        start
          ="2"
                   
          if  zlxz=='有振鈴下載' and  qqxz=='無全曲下載'  :
                        start
          ="3"
                   
          if  zlxz=='無振鈴下載' and  qqxz=='無全曲下載'  :
                        start
          ="4"
                   cur.execute(updateSql,(start,music,actor)  )
                   
          if not conn.total_changes==updateRow:
                        updateRow
          =conn.total_changes
                        log 
          = "[success=%s update=%s] %s-%s:%s %s" % (start,conn.total_changes,actor,music,zlxz,qqxz)
                   
          else :
                        log 
          = "[giveUp start=%s] %s-%s:%s %s" % (start,actor,music,zlxz,qqxz)
                   
          print bmcc(log)
                   os.system(
          " echo %s >> log " % (bmcc(log)) )
                   conn.commit()
                   
          return updateRow
               
          except Exception, myError:
                    excType, excValue, traceBack 
          = sys.exc_info()
                    
          print excType
                    
          print myError          

                    
          = open( "all_comm2.txt""r")
          cc
          =x.readline().split('\t')
          myrows
          =0
          updateRow 
          = 0 ;
          while cc :
              myrows
          =myrows+1
              
          try:
                  
          print "rows = %s " % (myrows)
                  cc
          =bm(x.readline()).split('\t')
                  updateRow
          =getValues(cc[6],cc[5],cc[21],cc[18],updateRow)
              
          except Exception, myError:
                  excType, excValue, traceBack 
          = sys.exc_info()
                  
          print excType
                  
          print myError   






          整理 www.aygfsteel.com/Good-Game
          posted on 2009-04-23 10:09 劉凱毅 閱讀(1369) 評論(0)  編輯  收藏 所屬分類: python
          主站蜘蛛池模板: 岗巴县| 江川县| 大英县| 沐川县| 错那县| 巴青县| 柘城县| 溆浦县| 嵊泗县| 石河子市| 宜君县| 涪陵区| 阜宁县| 宜兴市| 米泉市| 宿迁市| 台中市| 嵊州市| 高陵县| 敖汉旗| 昌都县| 杨浦区| 资阳市| 横山县| 莒南县| 揭阳市| 永靖县| 宽甸| 大港区| 禄劝| 河北区| 长白| 岚皋县| 江北区| 剑阁县| 泉州市| 沁源县| 蓬安县| 卫辉市| 灵石县| 宝鸡市|