2021-07-21山东大学暑期实训第四周3
作者:互联网
文章目录
一、数据处理
import csv
import pymysql
def clean():
with open("D:/test0721.csv", "r", encoding = "utf-8") as f:
reader = csv.reader(f)
rows = [row for row in reader]
info=[]
#print(rows)
for i in range(1,len(rows)):
#print(rows[i])
rol=[]
rol.append(i+29382)
rol.append("huawei")
rol.append(int(rows[i][7][:-1]))
rol.append("无")
rol.append(rows[i][1])
if ',' in rows[i][11]:
if rows[i][11][1]==',':
price = int(rows[i][11][0])*1000+int(float(rows[i][11][2:]))
elif rows[i][11][2]==',':
price = int(rows[i][11][0:2]) * 1000 + int(float(rows[i][11][3:]))
elif rows[i][11][3]==',':
price = int(rows[i][11][0:3]) * 1000 + int(float(rows[i][11][4:]))
elif rows[i][11]=='':
price=0
else: price=int(float(rows[i][11]))
rol.append(price)
#print(price)
rol.append(1)
rol.append("无")
rol.append(rows[i][6])
rol.append(int(rows[i][8][:-2]))
rol.append("2021-07-21")
info.append(rol)
return info
info=clean()
print(info[0])
db = pymysql.connect(host="", user="",
password="", database="")
cur = db.cursor()
for i in range(len(info)):
sql = "INSERT INTO huawei_product VALUES ({},'{}',{},'{}','{}',{},{},'{}','{}',{},'{}')"\
.format(info[i][0],info[i][1],info[i][2],info[i][3],info[i][4],info[i][5],info[i][6],info[i][7],info[i][8],info[i][9],info[i][10])
try:
# 执行sql语句
cur.execute(sql)
# 提交到数据库执行
db.commit()
print("成功",i)
except:
# Rollback in case there is any error
db.rollback()
print("失败",i)
二、爬取的数据
共有4933条数据
标签:info,11,rows,rol,07,int,实训,append,21 来源: https://blog.csdn.net/weixin_45607190/article/details/119089565