我正在构建一个python代码,以使用熊猫验证给定CSV文件中的电子邮件地址和电话号码,我想编写一个仅包含验证值的单独CSV文件。我是python的新手,并且为以下功能编写了代码:
from email_validator import validate_email, EmailNotValidError
import pandas as pd
import re
file = r'sample.csv'
filtered = r'filtered.csv'
valid = r'Valid.csv'
df=pd.read_csv(file)
def eVali(dataFrame):
try:
validate_email(dataFrame)
return True
except EmailNotValidError:
return False
def phoneValid(dataFrame):
if re.search("\w{3}-\w{3}-\w{4}",dataFrame):
return True
else:
return False
df["Email_validate"] = df['email'].apply(eVali)
df_fltrd = df[df['Email_validate']]
#del df_fltrd['Email_validate']
print(df_fltrd)
df_fltrd["Phone_validate"] =df_fltrd['phone'].apply(phoneValid)
df_valid = df_fltrd[df_fltrd["Phone_validate"]]
del df_valid["Phone_validate", "Email_validate"]
print(df_valid)
df_fltrd.to_csv(filtered)
df_valid.to_csv(valid)
此代码工作正常,我可以根据需要创建具有经过验证的值的新CSV。但是当我尝试使用适当的方法将这段代码组织为适当的类时,会出现错误消息
Traceback (most recent call last):
File "E:\myTasks\validator.py", line 7, in <module>
class Validator:
File "E:\myTasks\validator.py", line 47, in Validator
validation(self.file)
AttributeError: module 'self' has no attribute 'file'
这是我创建的课程。
Validator.py
import self as self
from email_validator import validate_email, EmailNotValidError
import pandas as pd
import re
class Validator:
def __init__(self):
self.file = r'sample.csv'
self.outFile =r'filteredSample.csv'
def emailValid(dataframe):
try:
validate_email(dataframe)
return True
except EmailNotValidError:
return False
def phoneValid(dataframe):
if re.search("\w{3}-\w{3}-\w{4}", dataframe):
return True
else:
return False
def validation(self):
df = pd.read_csv(self.file)
df = df.copy();
df["Email_validate"] = df['email'].apply(Validator.emailValid)
df_filtered = df[df['Email_validate']]
print(df_filtered)
df_filtered["Phone_validate"] = df_filtered['phone'].apply(Validator.phoneValid)
df_valid = df_filtered[df_filtered["Phone_validate"]]
del df_valid["Email_validate"]
del df_valid["Phone_validate"]
print(df_valid)
df_valid.to_csv(self.outFile)
validation(self)
有人可以帮我这个忙吗?我们将不胜感激。预先感谢!
答案 0 :(得分:1)
好吧,您不能从类本身调用实例方法
validation(self)
该位应该在您的类之外,例如,可以在实例化Validator对象之后从主函数中调用它。
my_validator = Validator()
my_validator.validation()
答案 1 :(得分:1)
您不导入自己。 self 是您在执行代码时所在的实例。
您的问题是您还不了解课程。您试图在python可以但脚趾不喜欢的类中调用类方法。
我建议您看看https://docs.python.org/3/tutorial/classes.html和/或https://www.w3schools.com/python/python_classes.asp。
您要将最后一行推到最后并添加
def main():
i = Validator()
i.validation()
if __name__ == "__main__":
main()