好的,所以这段代码适用于在线课程,我觉得这个问题是由Notepad ++引起的,但我无法证明什么。
实际错误是:
[Anaconda2] C:\Users\ia566\Desktop\Python>python countorgdb.py
File "countorgdb.py", line 20
print pieces
^
IndentationError: unexpected indent
这是Notepad ++的图片:
Notepad++ Screen Shot With Error
注意文件树的更改方式。对代码的任何添加都会导致文件树中出现额外的分支,从而导致Indention Error。
完整代码:
import sqlite3
conn = sqlite3.connect('jbemaildb.sqlite')
cur = conn.cursor()
cur.execute('''
DROP TABLE IF EXISTS Counts''')
cur.execute('''
CREATE TABLE Counts (org TEXT, count INTEGER)''')
fname = raw_input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'mbox-short.txt'
fh = open(fname)
for line in fh:
if not line.startswith('From: ') : continue
pieces = line.split()
org = pieces[1]
print org
# Here is were I a am trying to add a print statement and more code.
cur.execute('SELECT count FROM Counts WHERE org = ? ', (org, ))
row = cur.fetchone()
if row is None:
cur.execute('''INSERT INTO Counts (org, count)
VALUES ( ?, 1 )''', (org, ) )
else :
cur.execute('UPDATE Counts SET count=count+1 WHERE org = ?',
(org, ))
# This statement commits outstanding changes to disk each
# time through the loop - the program can be made faster
# by moving the commit so it runs only after the loop completes
conn.commit()
# https://www.sqlite.org/lang_select.html
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10'
print
print "Counts:"
for row in cur.execute(sqlstr) :
print str(row[0]), row[1]
cur.close()
我无法弄清楚我在做什么让Notepad ++如此愤怒。我只是希望能够添加到我的代码中。
答案 0 :(得分:0)
如果未正确使用空格或制表符,则会出现缩进错误。您应该用四个空格缩进代码。试一试,看看它是否有帮助。