Python代码在名字、姓氏和电子邮件地址之间输出一个新行,如何阻止这种情况发生?e、 g
ogchu
@gmail.com
ogchu
@yahoo.com
ogchu
@hotmail.com
ogchu
@aol.com
ogchu
@bigpond.com
first.txt和last.txt文件分别有大约2000个和100个名称,因此手动浏览并执行操作实际上不是一个选项
代码的目的是尝试生成看起来真实的电子邮件地址,有时使用firstname和lastname,有时使用1个首字母和一个lastname,有时使用2个首字母和一个lastname
代码:
import random
import string
import time
count = 0
while count < 50:
x = random.randint(0, 1)
y = random.randint(0, 1)
z = random.randint(0, 1)
if x == 0:
prefix1 = random.choice(string.ascii_lowercase)
prefix2 = random.choice(string.ascii_lowercase)
first = ""
if x == 1:
prefix1 = random.choice(string.ascii_lowercase)
prefix2 = ""
first = ""
if x == 1 and y == 1:
prefix1 = ""
prefix2 = ""
first = random.choice(open('first.txt').readlines())
last = random.choice(open('last.txt').readlines())
print(prefix1 + prefix2 + first + last + "@gmail.com")
print(prefix1 + prefix2 + first + last + "@yahoo.com")
print(prefix1 + prefix2 + first + last + "@hotmail.com")
print(prefix1 + prefix2 + first + last + "@aol.com")
print(prefix1 + prefix2 + first + last + "@bigpond.com")
print(prefix1 + prefix2 + first + last + "@icloud.com")
print(prefix1 + prefix2 + first + last + "@outlook.com")
count = count + 1
time.sleep(3)
问题在于
及
读一行时,最后一个字符是
\n
(换行符)。您需要通过调用.strip()
方法将其删除,如下所示:相关问题 更多 >
编程相关推荐