Pandas讀取csv匯入Mysql

2021-10-04 04:28:47 字數 1909 閱讀 1657

csv的格式表頭必須跟資料庫的型別一樣

插入**如下:

#coding:utf-8

import pandas as pd

import time

from sqlalchemy import create_engine

import pymysql

if __name__=='__main__':

start=time.time()

engine = create_engine('mysql+pymysql://root:[email protected]:3306/jingweidu?charset=utf8')

data = pd.read_csv(r'd:\image\necas\20200203\point\test3.csv', encoding='utf-8')

# sql = '''

# select * from pointtest;

# '''

# df = pd.read_sql_query(sql, engine)

# df = pd.dataframe()

# df2 = pd.dataframe()

# # 將新建的dataframe儲存為mysql中的資料表test,不儲存index列

print('write to mysql table successfully!')

# 輸出employee表的查詢結果

# print(df)

# print(time.time()-start)

# end=time.time()

# print(end-start)

讀取mysql

#coding:utf-8

import pymysql

import numpy

if __name__=='__main__':

db = pymysql.connect("62.234.49.137", "root", "!qaz2wsx", "jingweidu")

# 使用 cursor() 方法建立乙個游標物件 cursor

cursor = db.cursor()

# 使用 execute() 方法執行 sql 查詢

sql="select * from `pointtest`"

# sql = """insert into pointtest(longitude,

# latitude, z)

# values (132.7768374,33.98393204,0.863)"""

cursor.execute(sql)

# db.commit()

# data = cursor.fetchone()

# print(data)

# cursor.execute("select * from `0-5_reducepoint` where longitude>=132.775776 and longitude<=132.775777 and latitude>= 33.984254 and latitude<= 33.984255")

# 關閉資料庫連線

results = cursor.fetchall()

# list=

for item in results:

print(item)

# temp=

# for item2 in list:

# print(item2)

db.close()

pandas讀取csv檔案

import pandas as pd pd.set option display.max columns none 在pycharm下可以顯示所有列 pd.set options display.max rows none 在pycharm下可以顯示所有行 給每一列命名,注意第一行的設定 data...

pandas讀取csv檔案

1.匯入的包 import pandas as pd import numpy as np 2.讀取檔案我主要使用的是csv檔案格式,內容為如下 將資料加入到指定路徑中,然後進行讀檔案 def read csv path f open path,encoding utf 8 read data pd...

Pandas讀取CSV檔案

csv檔案自帶列標題 csv檔案無自帶列標題 在任意資料夾下建乙個csv檔案,如圖下所示。匯入 import pandas as pddata pd.read csv c mypatent ensemble1e0669 ie6690pl.csv c mypatent ensemble1e0669 i...