# pandas读取excel中得数据,并将数据存入mysql中
import pandas as pd
# from pandas import DataFrame
import psycopg2 as psycopg2
from sqlalchemy import create_engine
# 初始化引擎
connect = create_engine('postgresql+psycopg2://' + 'yonghuming' + ':' + 'mima' + '@ip' + ':'+duankouhao+'/'+shujukumingcheng)
# 读
data = pd.read_excel('./test.xlsx', sheet_name='sheet1')
# 重新定义列名,列名数量要和实际读取的sheet中列一致
data.columns = ['id', 'country', 'iso', 'name', 'introduce', 'city', 'continent', 'area', 'rank', 'job_title',
'department_en', 'department_cn', 'company', 'contact', 'other_remarks', 'link', 'avatar', 'Unnamed']
for key in data.columns:
print(key)
# 查看所有的值
# print(data.values)
# if_exists='replace' #覆盖入库
# if_exists='append' #增量入库
df = pd.DataFrame(data.values, columns=data.columns)
# print(df.info)
df.set_index("id", inplace=True)
pd.io.sql.to_sql(df, '表名', connect, schema='public', if_exists='replace', index=True, index_label='id')