csv_to_sql.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import pandas as pd
  2. import dataset
  3. import pymysql
  4. pymysql.install_as_MySQLdb()
  5. # df = pd.read_csv(r"C:\/Users\/s1301\/Documents\/關鍵字建議.csv",engine='python')
  6. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  7. table=db['seo_jobs']
  8. # table=db['selected_kw']
  9. client='泡麵'
  10. domain=''
  11. # for index,row in df.iterrows():
  12. # with open("C:\/Users\/s1301\/Documents\/新飛國際遊學SEO - 關鍵字12.08.csv") as f:
  13. # data_all = f.readlines()
  14. # print(data_all)
  15. f = open("C:\/Users\/s1301\/Documents\/456.csv",encoding='utf-8')
  16. # df = pd.read_csv(f,header=None, names=['kw', 'url'])
  17. df = pd.read_csv(f,header=None, names=['prefix', 'positive','rnd','URL'])
  18. df=df.fillna('')
  19. # print(df)
  20. url='cliqproducts.com.tw'
  21. for index,row in df.iterrows():
  22. # print(row['kw'],url)
  23. prefix='"'+row['prefix']+'"'
  24. positive='"'+row['positive']+'"'
  25. domain='"'+row['URL']+'"'
  26. rnd='"'+str(row['rnd'])+'"'
  27. postfix='""'
  28. data = f'"prefix":{prefix},"postfix":{postfix},"positive":[{positive}],"domain":[{domain}],"rnd":[{rnd}]'
  29. json='{'+data+'}'
  30. print(json)
  31. table.insert({'cust':client,'plan':'形象SEO','json':json})
  32. # table.insert({'term':row['kw'],'client':client,'domain':url})
  33. {"prefix": "護佳", "postfix": "", "domain": ["www.hujiai-international.com"], "positive": ["細胞食物","紅蔘"], "rnd": [""]}
  34. db.close()
  35. ####先從雲端下載csv 再用記事本打開另存一個csv#########