import codecs import sys import os fr=codecs.open('c:/data/dec_db.csv','r','utf-8') lines=fr.readlines() print(lines[0:5]) fr.close() addr_dict={} all_records=[] def gen_card(r): cont='' cont+='### '+r['name']+'\n\n' cont+=' * 統一編號: '+r['id']+'\n' cont+=' * 公司名稱: '+r['name']+'\n' cont+=' * 公司地址: '+r['addr']+'\n' cont+=' * 設立狀態: '+r['status']+'\n\n' return cont def gen_record(r,rlist): cont='+++\n' cont+='title = " '+r['name']+' "\n' cont+='date = "2021-06-13T09:07:20+08:00"\n' cont+='tags = ["'+r['addr'][0:3]+'室內設計" ]\n' cont+='categories = ["'+r['addr'][0:3]+'" ]\n' cont+='author = "幸福經紀人"\n' cont+='draft = false\n' cont+='id="'+r['id']+'"\n' cont+='+++\n\n' cont+='## 室內設計基本資料\n\n' cont+=gen_card(r) cont+='## 附近室內設計推薦\n\n' for rr in rlist: if rr['id']!=r['id']: cont+=gen_card(rr) return cont # print(cont) # sys.exit() def ins_dict(r): global addr_dict key=r['addr'][0:9] if addr_dict.get(key) is None: addr_dict[key]=[r] else: addr_dict[key].append(r) #for l in lines[0:300]: for l in lines[1:]: l=l.replace("'",'').replace('\n','') elmts=l.split(',') record={} record['id']=elmts[0] record['name']=elmts[1] record['addr']=elmts[2] record['status']=elmts[3] ins_dict(record) if '核准' in record['status']: all_records.append(record) maxlen=0 for k,v in addr_dict.items(): for vv in v: cont=gen_record(vv,v) fw=codecs.open('C:/gitlab/hhh_post/webSite/content/designer/'+vv['id']+'.md','w','utf-8') fw.write(cont) fw.close() # l=len(v) # if l>maxlen: # maxlen=l #print(maxlen) #print(all_records)