import csv import sys,os import Mysqldb def read_csv(filename): with open(filename) as f: f_csv = csv.
import csv
import sys,os
import Mysqldb
def read_csv(filename):
with open(filename) as f:
f_csv = csv.reader(f)
headers = next(f_csv)
#数据格式[1111,22222,1111,1111,.....]
#for row in f_csv:
# Process row
# field1=row[1]
# ...
return headers
def conn_to_psto():
#mysql连接方法
#Mysqldb.connect()
#postgl连接
onn = MySQLdb.connect(database="testdb", \
user="postgres", \
passWord="pass123",\
host="127.0.0.1",\
port="5432")
cur = conn.cursor()
return cur
if __name__ == "__main__":
#传入文件路径或文件名
filename=sys.argv[1]
f_csv=read_csv(filename)
cur=conn_to_psto()
for row in f_csv:
# Process row
field1=row[1]
...
sql='''insert into .......(field1,field2,...) values(%,%,...) '''.fORMat(field1=field1,field2=field2.........))
cur.execute(sql)
conn.commit()
conn.close()
--结束END--
本文标题: python 从csv读数据到mysql
本文链接: https://lsjlt.com/news/185347.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0