實(shí)際問題
主要在編碼問題下。
我把這解決的辦法說下吧,主要這統(tǒng)一 編碼 utf8
0. 數(shù)據(jù)庫 導(dǎo)入 utf8
如果 導(dǎo)入文本 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")
就是統(tǒng)一編碼 ,我這統(tǒng)一 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 分割 導(dǎo)入
.separator "\t"
.import db2.data imusic
.separator ","
#導(dǎo)出
sqlite> .output a.txt
sqlite> select * from tab_xx;
sqlite> .output stdout
"""
conn = sqlite3.connect("ex1")
cur = conn.cursor()
#編碼轉(zhuǎn)換 (我這 在 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)
x = 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()
#編碼轉(zhuǎn)換 (我這 在 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
x = 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
整理 m.tkk7.com/Good-Game