first python script

  • 背景:剛好公司有一個(gè)刷數(shù)據(jù)的任務(wù),就上手了python,早該開(kāi)始了。
  • 小結(jié)知識(shí)點(diǎn)如下:
  • 1 python的默認(rèn)編碼是unicode,如果在程序中輸入中文就會(huì)有亂碼,如果文件中中文是utf-8編碼的,就可以用以下這種方式解碼。
import os
import codecs
path = os.getcwd();
print(path);
os.chdir('/Users/liuchaoqun01/Desktop')
print(os.getcwd());
with codecs.open('to see you', encoding='utf-8') as f:
    print(f.read()) 
  • 2 cc助攻的解碼問(wèn)題:文本文件是latin1編碼的中文,120w行。甚至蛋疼,一直不知道應(yīng)該怎么轉(zhuǎn)為utf-8。經(jīng)cc點(diǎn)化:
with open('d:/res.txt','wb') as w:
    with open(r'd:/entity.list.all.txt','rb') as f:
        for x in f:
            try:
                w.write(x.decode('gb2312').encode('utf8'))
            except Exception:
                w.write(b'\n')

還是一知半解,等待cc指正!

  • 3 這是最后成形的代碼,本質(zhì)上就是一個(gè)數(shù)據(jù)腳本,總共跑了1hour,120w數(shù)據(jù),其實(shí)最后發(fā)現(xiàn)120w的數(shù)據(jù)查詢太耗時(shí)了,一開(kāi)始就應(yīng)該用批量查詢!!
import sys,re,os
import datetime
import MySQLdb
begin = datetime.datetime.now()
reload(sys)
sys.setdefaultencoding("utf-8")
path = os.getcwd();
os.chdir('/Users/liuchaoqun01/Documents/cloud/input')
predictFile = open('predict.trade2.entity.list.all', 'r')
count = 0
businessList = []
emptyList = []
predictFileList = []
resultList = []
emptyList = []
with open('res.txt','r') as w:
     for business in w:
          line = business.replace('\n', '')
          businessList.append(line)
for predict in predictFile:
     p = re.compile(r'\s|__')
     ss = p.split(predict)
     if len(ss) < 3:
          count = count + 1
          line = '-1'
     else:
          line = "select industryname from aodfeed.industry where industryid = " + (ss[2])
     predictFileList.append(line)
     line = ''
predictFile.close()
print 'The count of blank line is:' + str(count)

# mysql
outputResult = open('/Users/liuchaoqun01/Documents/cloud/output/result', 'w+')
outputEmpty = open('/Users/liuchaoqun01/Documents/cloud/output/empty', 'w+')
conn = MySQLdb.connect(host='10.99.196.227', port=8306, user='test', passwd='xxx', db='xxx', charset='utf8')
cursor = conn.cursor()
for i in range(len(predictFileList)):
     predict = predictFileList[i]
     business = businessList[i]
     if predict == '-1':
          emptyList.append(business + '\t' + '-1')
     else:
          sql = predict
          cursor.execute(sql)
          r = cursor.fetchall()
         # print (business + '\t' + r[0][0].decode('utf-8'))
          resultList.append(business + '\t' + r[0][0])

# write
for out in resultList:
     outputResult.write(out + '\n')
outputResult.close()

for out in emptyList:
     outputEmpty.write(out + '\n')
outputEmpty.close()
print 'process end!'
end = datetime.datetime.now()
print (end - begin)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容