# Reading CSV
import csv
with open('A453_datafile_4_Mat 4 Data File.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print (row)
import re
email=input("What is your email")
password= input("What is your password")
with open('A453_datafile_4_Mat 4 Data File.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['email'])
print(row['password'])
pattern = (row['email'])
if re.match(pattern, email):
print("Valid Email")
else:
print("Invalid Email")
pattern= (row['password'])
if re.match(pattern, password):
print("Valid Password")
else:
print("Invalid Password")
输出
['email', 'password', 'firstname', 'secondname', 'street', 'city', 'pcode']
['', '', '', '', '', '', '']
['ojones@coldmail.net', 'ocrabc', 'Oliver', 'Jones', '53 Vale House', 'Portsmouth', 'P03 2TD']
['', '', '', '', '', '', '']
['asmith@ablealarms.net', 'alan123', 'Alan', 'Smith', '3 Porter Street', 'Cricklewood', 'HE3 4DH']
['', '', '', '', '', '', '']
['rakhter@bluebell.org', 'raj45', 'Raj', 'Akhter', '12 Middleport Road', 'Cheltenham', 'CH4 5GH']
['', '', '', '', '', '', '']
['hrdg4678@netwise.net', 'ha123', 'Jan', 'Pietersson', '56 Holde Street', 'Birmingham', 'B23 3RT']
['', '', '', '', '', '', '']
['miguel5@bluebell.net', 'happy3', 'Miguel', 'Santos', '45 Vine Avenue', 'Oxford', 'OX7 3RF']
What is your emailojones@coldmail.net
What is your passwordalan123
ojones@coldmail.net
ocrabc
asmith@ablealarms.net
alan123
rakhter@bluebell.org
raj45
hrdg4678@netwise.net
ha123
miguel5@bluebell.net
happy3
Invalid Email
Invalid Password
当我输入我打印出来的csv文件中包含的数据时,我会收到无效的电子邮件和无效密码语句,但每当我从最后一组输入数据时,它都表示数据是虚假的。我不知道该怎么做,因为我还是python的初学者,我怀疑这与我定义2个模式有关。
答案 0 :(得分:0)
看起来像一个简单的缩进问题。
您需要缩进所有匹配和打印的行(从pattern = (row['email'])
开始):
for row in reader:
print(row['email'])
print(row['password'])
# BUG! only the above two statements get executed for every row
# BUG: so the final value of email = row['email'] for the last row!
# BUG: so since the following lines are unindented, they only get executed once,
# which is not what you want!
pattern = (row['email']) ...
if pattern == email: # or pattern.index(email) >= 0
# ...