下面的代码只能移动带有电子邮件地址的帐户,该电子邮件地址在"@"标志后面有2个大写字母。过滤器似乎会捕获所有内容。
我已经更改了该值返回用户名和check_email变量的程度。
mike@gmail.com
toTR@yahoo.com
erferTT@hotmail.com
fwefEE@yahoo.com
thisYY@aol.com
that@yahoo.com
应省略第一个和最后一个电子邮件地址,其余的电子邮件应放入新文件中,但也正在拉动小写电子邮件地址。
谢谢
import sys
import argparse
from datetime import datetime
def parse_arg():
"""Parses the args"""
if len(sys.argv) < 1:
print("Args: Input filename")
raise RuntimeError("Insufficient arguments.")
arg_1 = sys.argv[1]
return arg_1
#Parameter is the filename
filename = parse_arg()
#Create filename for output
output_file_date = datetime.now().strftime("%Y%m%d_%I%M%S")
output_file = output_file_date + '_' + filename
#Access the input file
with open(filename, 'rb') as input_file, open(output_file, 'w') as file_output:
#Create the header"""
output_file_header = input_file.readline().decode().replace('r','')
file_output.write(output_file_header)
#Skip the header in the input file
next(input_file)
#iterate over file object line by line
for line in input_file:
#Create output file objects
lines = line.decode().split(',')
#Create the email username variable
username = lines[1].split('@')[0]
#Create variable that display last two characters
check_email = username[-1:]
#Create file based on business rules and write results to file.
if check_email.isupper(): #and len(username) < 16:
output_data = ','.join(lines).replace('r','')
file_output.write(output_data)
您要在此处检查最后一个字符,而不是最后两个字符。
而不是写作 check_email = username[-1:]
做就是了 check_email = username[-2:]
定义的数字有多少个字母。
也一如既往地建议您从每行中打印出实际获得的东西,因此您可以更好地理解并看到更好的事情。