Read & Open a CSV File & Randomly Select an Entry
This is following the https://www.youtube.com/watch?v=NUVblHTElTk&index=3&list=PLEsfXFp6DpzR6FatOy4RtoXfu4PeYO_RL.
#-*-coding=utf-8-*-
import os
import random
import csv
import string
def get_file_path(filename):
currentdirpath=os.getcwd()
file_path=os.path.join(os.getcwd(),filename)
return file_path
path=get_file_path('choice.csv')
def get_random_string():
rnstring=''
for i in range(0,4)
rnstring += random.choice(string.ascii_letters)
return rnstring
def read_csv(filepath):
emails={}
i=0
with open(filepath,'rU') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if not 'Email Address' in row:
email, domain = row[0].split('@')
fake_email=email[:4] + get_random_string() + "@" + domain
emails[i] = fake_email
i += 1
return emails
def get_winner():
emails=read_csv(path)
ran_num=random.randint(0,len(emails)-1)
winner=emails[ran_num]
print winner,ran_num
get_winner()