In: Computer Science
1) Write a python program that opens a file, reads
all of the lines into a list of strings, and closes the file. Use
the Readlines() method. Test your programing using the names.txt
file provided.
2) Convert the program into a function called loadFile, that
receives the file name as a parameter and returns a list of
strings.
3) Write a main routine that calls loadFIle three times to load the
three data files given into three lists. Then choose a random
element from the title list, two from the name list, and one from
the descriptor list to generate a name. Print the name be in the
form shown (you have to add the "the"):
title name name the descriptor
For example:
King Ludovicus Botolf the Bowman
Print the name to the screen.
Submit your python file (.py) to Sakai
Part 2: Writing Files (Optional Extra Credit 20 pts)
Modify the program to generate 10 names and store them in a
list.
Write a function, dumpFile that writes the list to a file called
"CharacterNames.txt" There should be one Character Name on each
line in the file.
Test the program to be sure it works
Im stumped at number 2
here is what i have for number 1
f = open('names.txt', 'r')
lines = f.readlines()
for line in range(len(lines)):
lines[line] = lines[line].rstrip()
print(lines)
f.close()
Part 1:
1)
Python program that reads all the lines of a file into a list of strings and closes the file.
Program:
lines=[]
# Store each line in a list
with open('input.txt') as file:
lines = file.readlines()
# Remove new line in each string
lines = [i.strip() for i in lines]
file.close()
for i in lines:
print(i)
Input text file (input.txt):
Jack
Micheal
Alex
Bob
Sample output:
2)
The loadFile function receives the file name as a parameter and returns a list of strings.
Program:
def loadFile(fileName):
lines=[]
with open(fileName) as file:
lines = file.readlines()
lines = [i.strip() for i in lines]
file.close()
return lines
lines=loadFile('input.txt')
print (lines)
Input text file (input.txt):
Jack
Micheal
Alex
Bob
Sample output:
3)
The complete program along with main function.
Program:
import random
def loadFile(fileName):
lines=[]
with open(fileName) as file:
lines = file.readlines()
lines = [i.strip() for i in lines]
file.close()
return lines
def main():
# Call the loadFile function along with the text document as parameter
title=loadFile('title.txt')
name=loadFile('name.txt')
descriptor=loadFile('descriptor.txt')
# Generate random strings
rand_title=title[random.randint(0,len(title)-1)]
rand_name1=name[random.randint(0,len(name)-1)]
rand_name2=name[random.randint(0,len(name)-1)]
rand_desc=descriptor[random.randint(0,len(descriptor)-1)]
# Print the output
print(rand_title+" "+rand_name1+" "+rand_name2+" the "+rand_desc)
main()
Input text files:
title.txt:
Lord
King
Duke
Earl
Bishop
Count
Sir
Alderman
Admiral
Vicar
Monk
Archbishop
Prince
Cardinal
Chancellor
name.txt:
Ademar
Adelard
Aalart
Alart
Aldous
Aldis
Amaury
Aimeri
Amauri
Amery
Emericus
Alphonse
Alfan
Ancel
descriptor.txt:
Bald
Roses
Dragon
Lion Hearted
Bold
First
Second
Third
Fourth
Monk
Cute
Mighty
Bowman
Elf
Red
Strong
Dragonslayer
Giant
Sleuth
Evil
Car Salesman
Sample output:
Part 2:
Program:
import random
def loadFile(fileName):
lines=[]
with open(fileName) as file:
lines = file.readlines()
lines = [i.strip() for i in lines]
file.close()
return lines
def dumpFile(CharacterNames):
file=open("CharacterNames.txt","w+")
for i in CharacterNames:
# Write each line to a text file
file.write(i+"\n")
def main():
# Call the loadFile function along with the text document as parameter
title=loadFile('title.txt')
name=loadFile('name.txt')
descriptor=loadFile('descriptor.txt')
CharacterNames=[]
i=0
while i<10:
# Generate random strings
rand_title=title[random.randint(0,len(title)-1)]
rand_name1=name[random.randint(0,len(name)-1)]
rand_name2=name[random.randint(0,len(name)-1)]
rand_desc=descriptor[random.randint(0,len(descriptor)-1)]
# Store the string in a list
Charstr=rand_title+" "+rand_name1+" "+rand_name2+" the "+rand_desc
CharacterNames.append(Charstr)
i=i+1
# Call the dumpFile function
dumpFile(CharacterNames)
main()
Input text files:
title.txt:
Lord
King
Duke
Earl
Bishop
Count
Sir
Alderman
Admiral
Vicar
Monk
Archbishop
Prince
Cardinal
Chancellor
name.txt:
Ademar
Adelard
Aalart
Alart
Aldous
Aldis
Amaury
Aimeri
Amauri
Amery
Emericus
Alphonse
Alfan
Ancel
descriptor.txt:
Bald
Roses
Dragon
Lion Hearted
Bold
First
Second
Third
Fourth
Monk
Cute
Mighty
Bowman
Elf
Red
Strong
Dragonslayer
Giant
Sleuth
Evil
Car Salesman
Sample output: