在 python 中执行 executemany 命令时出现 KeyError
Getting KeyError while executing executemany command in python
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 文件被正确读取,因为我有打印行,如下所示。但是在数据库 table 中插入数据时会出错。我认为 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 文件
数据库table结构
查看您的输出,CSV 的列称为“露点”,在 "D" 之前有一个 space。像这样引用它(即 row[' Dew Point']
)应该可以修复错误。
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 文件被正确读取,因为我有打印行,如下所示。但是在数据库 table 中插入数据时会出错。我认为 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 文件
数据库table结构
查看您的输出,CSV 的列称为“露点”,在 "D" 之前有一个 space。像这样引用它(即 row[' Dew Point']
)应该可以修复错误。