csv_to_sql.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. # table=db['sns_kw']
  10. # table=db['select_kw']
  11. client='歌林'
  12. # domain='naturalbenefits-hpp'
  13. # for index,row in df.iterrows():
  14. # with open("C:\/Users\/s1301\/Documents\/新飛國際遊學SEO - 關鍵字12.08.csv") as f:
  15. # data_all = f.readlines()
  16. # print(data_all)
  17. f = open("/Users/mac/Downloads/_2024歌林關鍵字 - 工作表1.csv")
  18. # df = pd.read_csv(f,header=None, names=['kw', 'url'])
  19. df = pd.read_csv(f,header=None, names=['kw'])
  20. # df = pd.read_csv(f,header=None, names=['prefix','id', 'positive','domain','rnd'])
  21. df=df.fillna('')
  22. # print(df)
  23. domain='kolin.com.tw'
  24. lst=[]
  25. for index,row in df.iterrows():
  26. # print(row)
  27. # prefix='"'+row['prefix']+'"'
  28. # # positive='"'+row['positive']+'"'
  29. # positive=row['positive']
  30. # domain='"'+row['domain']+'"'
  31. # rnd='"'+str(row['rnd'])+'"'
  32. # postfix='""'
  33. # id=row['id']
  34. # data = f'"id":{id},"prefix":{prefix},"domain":[{domain}],"postfix":{postfix},"positive":[{positive}],"rnd":[{rnd}]'
  35. # json='{'+data+'}'
  36. # print(json)
  37. # table.insert({'cust':client,'plan':'形象SEO','json':json})
  38. table.insert({'term':row['kw'],'client':client,'domain':domain})
  39. # table.insert({'term': row['kw'], 'client': client, 'url': row['url']})
  40. db.close()
  41. ####先從雲端下載csv 再用記事本打開另存一個csv#########