for and in and enumerate and if … AHHHHHHH
我有一个涉及编程的学校项目。 我在批处理和python之间转移,因为这对我来说最简单。
这是我用python编写的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | for i,x in enumerate(result): if number in x: print"Start of 1" cursor.execute(insert1, (number)) cursor.execute(delete, (number)) f.close() os.system("extracommands.bat 1") print"Found Number" else: print"Start of 2" cursor.execute(insert1, (number)) cursor.execute(insert2, (number)) cursor.execute(delete, (number)) f.close() os.system("emailfine.py 1") print"Finished 2" |
我的问题是我在元组中找不到字符串。 结果是,当有结果时,它会完美运行。 但是当没有结果时,没有任何反应。
我怎么能克服这个?
先感谢您。
编辑:
在问我的问题时,我可能不够具体。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import MySQLdb import sys import os print"==========================" print"Start Registered_Or_Not.py" print"==========================" os.chdir("C:/DisabledParkingSpacesImages/textfiles") f = open("numberplate.txt","r") number = f.read() print number try: db = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'jordan', db = 'disabledparking' ) except Exception as e: sys.exit("Can't access the database") print"MySQL Connection OK" cursor = db.cursor() cursor.execute("SELECT registration FROM registered WHERE EXISTS (SELECT 1 FROM limbo WHERE limbo.registration = registered.registration)") result = cursor.fetchall() insert1 ="INSERT INTO log (registration, time, date) VALUES(%s, CURRENT_TIME, CURRENT_DATE);" insert2 ="INSERT INTO not_registered (registration, time, date) VALUES(%s, CURRENT_TIME, CURRENT_DATE);" delete ="DELETE FROM limbo WHERE registration=%s;" print"-------------" print"Result:" print result print"-------------" TrueFalse = False for i,x in enumerate(result): if number in x: print"Start of 1" cursor.execute(insert1, (number)) cursor.execute(delete, (number)) f.close() os.system("extracommands.bat 1") print"Found Number" TrueFalse = True elif TrueFalse == False: print"Start of 2" cursor.execute(insert1, (number)) cursor.execute(insert2, (number)) cursor.execute(delete, (number)) f.close() os.system("emailfine.py 1") print"Finished 2" db.commit() |
好的,我回答了自己的问题。
这是有问题的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | for i,x in enumerate(result): if number in x: print"Start of 1" cursor.execute(insert1, (number)) cursor.execute(delete, (number)) f.close() os.system("extracommands.bat 1") print"Found Number" else: print"Start of 2" cursor.execute(insert1, (number)) cursor.execute(insert2, (number)) cursor.execute(delete, (number)) f.close() os.system("emailfine.py 1") print"Finished 2" |
这是一个没有问题的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | for i,x in enumerate(result): if number in x: print"Start of 1" cursor.execute(insert1, (number)) cursor.execute(delete, (number)) f.close() os.system("extracommands.bat 1") print"Found Number" TrueFalse = True if TrueFalse == False: print"Start of 2" cursor.execute(insert1, (number)) cursor.execute(insert2, (number)) cursor.execute(delete, (number)) f.close() os.system("emailfine.py 1") print"Finished 2" |