2016-04-18 50 views
0

我正在处理一个Thinkful挑战。以下是说明:Python SQLITE上的意外缩进错误

编写一个名为“database.py”的脚本来打印出7月份是最暖和的月份的城市。脚本必须:

连接到数据库创建城市和天气表格(提示: 先通过语句DROP TABLE IF EXISTS;去除 表你执行CREATE TABLE ...语句之前)中插入数据两个表加入数据一起加载到一个数据帧大熊猫

以下是错误

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) 

我在做什么错?

您的帮助非常感谢!

+0

IndentationError可能是由于混合了制表符和空格。运行'python -t database.py'。 '-t'标志告诉python警告源文件是否混合了制表符和空格以进行缩进。 – unutbu

+0

如果事实证明IndentationError是由于混合了制表符和空格,可以使用[autope8或reindent.py](http://stackoverflow.com/a/2625361/190597)将制表符转换为空格。 – unutbu

+0

这绝对是问题。似乎发生在我从一个.py文件复制并粘贴到另一个时发生。谢谢! – Aschharwood

回答

0

事实证明,这是我的文本编辑器Sublime Text中的复制粘贴问题。我将一些代码从一个文件复制到另一个文件。虽然它看起来不像是一个缩进问题,但文本编辑器似乎正在阅读它。