2014-12-03 97 views
7

电子邮件验证不支持的操作:不写蟒蛇

#Email validator 
import re 

f= open ('ValidEmails.txt', 'w') 

def is_email(): 
    email=input("Enter your email") 
    pattern = '[\.\w]{1,}[@]\w+[.]\w+' 
    file = open('ValidEmails.txt','r') 
    if re.match(pattern, email): 
     file.write(email) 

     file.close 
     print("Valid Email") 
    else: 
     print("Invalid Email") 

#The Menu   
print("The Email validator progam \n") 
print("What do you want to do\n") 
print("Validate the Email") 
print("Quit") 

while True: 
     answer=(input("Press V, or Q : ")) 
     if answer in("V" ,"v"): 
      is_email() 
     elif answer in("Q" ,"q"): 
      break 
     else: 
      print("Invalid response") 

我很奇怪,为什么我的数据不会写入disk.Python说,我的操作不被支持。

is_email 
    file.write(email) 
io.UnsupportedOperation: not writable 

我应该转换电子邮件为一个字符串这样或

file.write(str(email)) 

是别的东西

我可能失去了一些东西很简单。

+0

请包括您看到的错误的完整追溯。 – 2014-12-03 18:14:06

回答

21

您打开变量“文件”作为只读然后尝试写入它。使用'w'标志。

file = open('ValidEmails.txt','w') 
... 
file.write(email) 
0
file = open('ValidEmails.txt','wb') 
file.write(email.encode('utf-8', 'ignore')) 

这是解决你的encode error也。