【发布时间】:2016-04-18 22:00:37
【问题描述】:
我正在处理一项有思想的挑战。以下是说明:
编写一个名为“database.py”的脚本来打印出城市 七月是最热的月份。您的脚本必须:
连接到数据库创建城市和天气表(提示: 首先通过语句 DROP TABLE IF EXISTS ;去除 执行CREATE TABLE ...语句之前的表)将数据插入两个表中将数据连接在一起加载到pandas DataFrame中
这是错误
Aschs-MacBook-Air:dbs aschharwood$ python database.py
File "database.py", line 20
cols = [desc[0] for desc in cur.description]
^
IndentationError: unexpected indent
代码如下:
import sqlite3 as lite
# Here you connect to the database. The `connect()` method returns a connection object.
con = lite.connect('get_started.db')
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS cities")
cur.execute("DROP TABLE IF EXISTS weather")
cur.execute("CREATE TABLE cities (name text, state text)")
cur.execute("CREATE TABLE weather (city text, year integer, warm_month text, cold_month text)")
cur.execute("INSERT INTO cities VALUES('Washington', 'DC')")
cur.execute("INSERT INTO cities VALUES('Houston', 'TX')")
cur.execute("INSERT INTO weather VALUES('Washington', 2013, 'July', 'January',)")
cur.execute("INSERT INTO weather VALUES('Houston', 2013, 'July', 'January',)")
cur.execute("SELECT * FROM cities LEFT OUTER JOIN weather ON name = city")
rows = cur.fetchall() #grabbing all the rows
cols = [desc[0] for desc in cur.description]
df = pd.DataFrame(rows, columns=cols)
print(df)
我做错了什么?
非常感谢您的帮助!!!
【问题讨论】:
-
IndentationError 可能是由于混合了制表符和空格。运行
python -t database.py。-t标志告诉 python 如果源文件混合了制表符和空格以进行缩进,则发出警告。 -
如果 IndentationError 是由于混合了制表符和空格,您可以使用autope8 or reindent.py 将制表符转换为空格。
-
这绝对是问题所在。当我从一个 .py 文件复制并粘贴到另一个文件时,似乎发生了这种情况。谢谢!