gendata.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import codecs
  2. import sys
  3. import os
  4. fr=codecs.open('c:/data/dec_db.csv','r','utf-8')
  5. lines=fr.readlines()
  6. print(lines[0:5])
  7. fr.close()
  8. addr_dict={}
  9. all_records=[]
  10. def gen_card(r):
  11. cont=''
  12. cont+='### '+r['name']+'\n\n'
  13. cont+=' * 統一編號: '+r['id']+'\n'
  14. cont+=' * 公司名稱: '+r['name']+'\n'
  15. cont+=' * 公司地址: '+r['addr']+'\n'
  16. cont+=' * 設立狀態: '+r['status']+'\n\n'
  17. return cont
  18. def gen_record(r,rlist):
  19. cont='+++\n'
  20. cont+='title = " '+r['name']+' "\n'
  21. cont+='date = "2021-06-13T09:07:20+08:00"\n'
  22. cont+='tags = ["'+r['addr'][0:3]+'室內設計" ]\n'
  23. cont+='categories = ["'+r['addr'][0:3]+'" ]\n'
  24. cont+='author = "幸福經紀人"\n'
  25. cont+='draft = false\n'
  26. cont+='id="'+r['id']+'"\n'
  27. cont+='+++\n\n'
  28. cont+='## 室內設計基本資料\n\n'
  29. cont+=gen_card(r)
  30. cont+='## 附近室內設計推薦\n\n'
  31. for rr in rlist:
  32. if rr['id']!=r['id']:
  33. cont+=gen_card(rr)
  34. return cont
  35. # print(cont)
  36. # sys.exit()
  37. def ins_dict(r):
  38. global addr_dict
  39. key=r['addr'][0:9]
  40. if addr_dict.get(key) is None:
  41. addr_dict[key]=[r]
  42. else:
  43. addr_dict[key].append(r)
  44. #for l in lines[0:300]:
  45. for l in lines[1:]:
  46. l=l.replace("'",'').replace('\n','')
  47. elmts=l.split(',')
  48. record={}
  49. record['id']=elmts[0]
  50. record['name']=elmts[1]
  51. record['addr']=elmts[2]
  52. record['status']=elmts[3]
  53. ins_dict(record)
  54. if '核准' in record['status']:
  55. all_records.append(record)
  56. maxlen=0
  57. for k,v in addr_dict.items():
  58. for vv in v:
  59. cont=gen_record(vv,v)
  60. fw=codecs.open('C:/gitlab/hhh_post/webSite/content/designer/'+vv['id']+'.md','w','utf-8')
  61. fw.write(cont)
  62. fw.close()
  63. # l=len(v)
  64. # if l>maxlen:
  65. # maxlen=l
  66. #print(maxlen)
  67. #print(all_records)