Python小練習

一個小工具,發現有一個開源的詩詞庫(https://github.com/michaelliao/shici/tree/master/web/src/main/resources/text),考慮導入到數據庫中,詩詞庫是通過文件夾方式組織,所以用python發現很快的實現這個需求,代碼記錄如下:
   

 


#導入依賴庫

import os,sys  
from datetime import datetime
import pymysql



def readpoem(path):
    str=''
    f = open(path, 'r')               
    line = f.readline()                 
    while line: 
        oldline = line
        line = f.readline() 
        if 'form' in oldline:
            continue
        if 'tags' in oldline:
            continue
        if oldline.strip()=='':
            continue
        #oldline=oldline.strip('\n')
        str += oldline 
    f.close() 
    return str

def readmeta(path):
    str=''
    f = open(path, 'r')               
    line = f.readline()                 
    while line: 
        oldline = line
        line = f.readline() 
        if 'birth' in oldline:
            continue
        if 'death' in oldline:
            continue
        if oldline.strip()=='':
            continue
        oldline=oldline.strip('\n')
        str += oldline 
    f.close() 
    return str

def listdir(path, list_result):  
    result={}
    containFile=False
    for file in os.listdir(path):  
        file_path = os.path.join(path, file)  
        if os.path.isdir(file_path):  
            listdir(file_path, list_result)  
        elif os.path.splitext(file_path)[1]=='.txt':
            if 'meta' in file_path:
                meta = readmeta(file_path)
                if meta.strip()!='':
                    result['meta']=meta 
            else:
                #filepath,fullflname = os.path.split(file_path)
                #fname,ext = os.path.splitext(fullflname)
                containFile=True 

                content = readpoem(file_path)
                if result.get('poem', None) is None:
                    result['poem']=[]
                poem_detail={}

                if content.strip()!='':
                    poem_detail['content'] =content.replace('\'', '')

                #Get the poem name
                poem_name=os.path.splitext(file_path)[0]
                poem_temp_array = poem_name.split('/')
                poem_titles = poem_temp_array[-3:]
                index = 0
                for title in poem_titles:
                    if poem_detail.get('title', None) is None:
                        poem_detail['title'] = '[{0}]'.format(title.split('.')[1].replace('\'', ''))
                    else:
                        if index == 1:
                            poem_detail['title'] += title.strip()
                        else:
                            poem_detail['title'] += '-{0}'.format(title.strip())                            
                    index += 1

                #wirite the poem detail info
                result['poem'].append(poem_detail);
                #print('name:{0}, content:{1}'.format(poem_detail['title'], poem_detail['content']))
            #list_result.append(file_path)
        else:
            pass
    if containFile:
        print('poem.numb:{0}'.format(len(result['poem'])))
    else:
        pass
    if len(result) >0:
        list_result.append(result)
    else:
        pass



defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)

result_poems=[]
listdir(sys.path[0], result_poems)

print("size:%d" %len(result_poems))
#for item in result_file:
#    print(item.get('meta', ''))
#    for poem in item['poem']:
#        print('name:{0}, content:{1}'.format(poem['title'], poem['content']))



#寫入數據庫

#from datetime import datetime
#connect db
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='python', db='myschool', charset='utf8')

cur = conn.cursor() 

dt=datetime.now()  
now =  dt.strftime( '%Y%m%d%H' )

#注意轉義符
sql = "insert into shici(`name`,`content`,`time`) values (\'{0}\', \'{1}\', {2})"


#for item in result['poem']:
#    insert_sql = match.format(item['title'], item['content'], )
#conn.close()
try:
    for item in result_poems:
            print(item.get('meta', ''))
            for poem in item['poem']:
                    print('name:{0}, content:{1}'.format(poem['title'], poem['content']))
            insert_sql = sql.format(poem['title'].encode('utf-8'), poem['content'].encode('utf-8'), now)
            cur.execute(insert_sql)
            conn.commit()
except:
    conn.rollback()


conn.commit()
conn.close()

  

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