csv_to_sql.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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. client='護佳'
  9. domain=''
  10. # for index,row in df.iterrows():
  11. # with open("C:\/Users\/s1301\/Documents\/關鍵字建議.csv") as f:
  12. # data_all = f.readlines()
  13. f = open("C:\/Users\/s1301\/Documents\/關鍵字建議.csv")
  14. df = pd.read_csv(f)
  15. df=df.fillna('')
  16. # print(df)
  17. for index,row in df.iterrows():
  18. prefix='"'+row['prefix']+'"'
  19. positive='"'+row['positive']+'"'
  20. domain='"'+row['URL']+'"'
  21. rnd='"'+str(row['rnd'])+'"'
  22. postfix='""'
  23. data = f'"prefix":{prefix},"postfix":{postfix},"positive":[{positive}],"domain":[{domain}],"rnd":[{rnd}]'
  24. json='{'+data+'}'
  25. print(json)
  26. table.insert({'cust':client,'plan':'形象SEO','json':json})
  27. # table.insert({'term':row['kw'],'client':client,'domain':domain})
  28. {"prefix": "護佳", "postfix": "", "domain": ["www.hujiai-international.com"], "positive": ["細胞食物","紅蔘"], "rnd": [""]}
  29. db.close()