我是Python的超级新手,只是尝试使用随机电子邮件生成器。
我只是使用其中包含数据集的json文件,因此可能会有更好的方法来执行此操作。
我可以使脚本正常工作,但是我需要一些建议。我希望发件人的电子邮件与注销名相同。 即david_jones @ hotmail等来自Regards,David Jones。目前,我已经生成了单独的随机电子邮件和单独的注销名称。我需要链接两者。目前其他一切都还可以。
有人可以帮助我提供更好的方法吗? 代码:
import json
import random
f = open("C:/Users/*/Desktop/Email.txt", "a")
sentfrom = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Send.json').read())
send = sentfrom [random.randint(0,4)]
carboncopy = "CC:"
receiver = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/To.json').read())
to = receiver[random.randint(0,4)]
datesent = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Date.json').read())
date = datesent[random.randint(0,4)]
subjects = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Subject.json').read())
subject = subjects[random.randint(0,4)]
greetings = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Greeting.json').read())
greeting= greetings[random.randint(0,4)]
firstsentence = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Sent1.json').read())
sent1 = firstsentence[random.randint(0,4)]
secondsentence = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Sent2.json').read())
sent2 = secondsentence[random.randint(0,4)]
thirdsentence = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Sent3.json').read())
sent3 = thirdsentence[random.randint(0,4)]
fourthsentence = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Sent4.json').read())
sent4 = fourthsentence[random.randint(0,4)]
farewell = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Goodbye.json').read())
goodbye = farewell[random.randint(0,4)]
regards = json.loads(open('C:/Users/*/Desktop/*/Scripts/Test/Sender.json').read())
salutation = regards[random.randint(0,4)]
conversation = send +'\n'+ to +'\n'+ carboncopy +'\n'+ date +'\n'+ subject +'\n'+ '\n' + greeting +', \n'+ '\n' + sent1 +'\n'+ '\n' + sent2 +'\n'+'\n'+ sent3 +'\n'+'\n'+ sent4 +'\n'+'\n'+ goodbye +'\n'+'\n'+ salutation
f.write(conversation)
f.close()
预先感谢, 嗡嗡声
答案 0 :(得分:0)
假设regards
包含签收名称。
您要首先摆脱注销名称,而不要使用'Regards, John Doe'
,而要全部使用'Regards', 'Best', 'Thanks!'
等。也许只是创建一个列表,而不是从json中读取它:
regards = ['Regards,', 'Best,', 'Thanks!' ...]
假设每个人在电子邮件中的格式都相同,即john_doe@whatever.com
,则可以从中获取名称:
my_name = to.split('@')[0].replace('_', ' ').title()
# my_name will be 'John Doe'
然后在my_name
之后将conversation
添加到salutation
。