Python代码
import pymysql
import xlrd
import re
import os
import csv
conn = pymysql.connect(host='',
user='',
password='',
db='',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
sql = "INSERT INTO `webscrap_wunderground` (Date,MeanTemp,MaxTemp,MinTemp,HeatingDegree,DewPoint,MeanHumidity,MaxHumidity,MinHumidity,Precipitation,SeaLevel,MeanWindSpeed,MaxWind_Speed,Visibility,Event) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
with open('C:/Users/SARIKA/Weather Data.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
cur = conn.cursor()
cur.executemany(sql,[(row['Date'],row['Mean Temperature'],row['Max Temperature'],row['Min Temperature'],row['Heating Degree Days'],row['Dew Point'],row['MeanHumidity'],row['MaxHumidity'],row['MinHumidity'],row['Precipitation'],row['SeaLevel'],row['MeanWindSpeed'],row['MaxWindSpeed'],row['visibility'],row['Events'])])
conn.escape_string(sql)
conn.commit()
这是我执行上述代码时遇到的错误。CSV 文件被正确读取,因为我有打印行,如下所示。但是在数据库表中插入数据时会出现错误。我认为 csv 和数据库之间的映射存在问题,但我找不到错误,请帮忙。
OrderedDict([('Date', '2017/1/1'), ('Mean Temperature', '24'), ('Max Temperature', '31'), ('Min Temperature', '16'), ('Heating Degree Days', 'N/A'), (' Dew Point', '16'), (' Average Humidity', '53'), (' Max Humidity', '83'), (' Minimum Humidity', '27'), (' Precipitation', '0.0'), (' Sea Level Pressure', '1012.73'), (' Average Wind Speed', '4'), (' Maximum Wind Speed', '17'), (' Visibility', '2.0'), (' Events', '')])
Traceback (most recent call last):
File "C:\Users\SARIKA\eclipse-workspace\a1\csvtosql.py", line 20, in <module>
cur.executemany(sql,[(row['Date'],row['Mean Temperature'],row['Max Temperature'],row['Min Temperature'],row['Heating Degree Days'],row['Dew Point'],row['MeanHumidity'],row['MaxHumidity'],row['MinHumidity'],row['Precipitation'],row['SeaLevel'],row['MeanWindSpeed'],row['MaxWindSpeed'],row['visibility'],row['Events'])])
KeyError: 'Dew Point'
CSV 文件
数据库表结构