简体   繁体   中英

Change Specific Words in Text File With User Inputs

I have been following the 'Automate The Boring Stuff' Python course. One practice project requires a passage of text in a txt file to be changed according to some user inputs.

My attempt is below but my way of thinking is still very much a long winded way of getting the job done. I don't feel my code is the most efficient 'pythonic' method.

Is there a better, more efficient way to do this?

Any feedback is appreciated (I left my notes in the code for some clarity).

Replace the words ADJECTIVE, NOUN, ADVERB and NOUN in a passage of text.
Use prompts to input the words to be used.
Print to sceen and save to file.

#Prompt user for the words to add
from pathlib import Path

file = open('Mad Libs.txt', 'w')
file.write('The ADJECTIVE panda walked to the NOUNONE and then VERB. A nearby NOUNTWO was unaffected 
by these events.')
file.close()

print('Enter an adjective: ')
a = input()
print('Enter a noun: ')
nOne = input()
print('Enter a verb: ')
v = input()
print('Enter a noun: ')
nTwo = input()

#Open the file and split the passage into a list??
file = open('Mad Libs.txt')
text = file.read().split()

#Find words in file to be replaced.
aIndex = text.index('ADJECTIVE')
nOneIndex = text.index('NOUNONE')
vIndex = text.index('VERB.')
nTwoIndex = text.index('NOUNTWO')

#Replace each word with the inputs and close.
text[aIndex] = a
text[nOneIndex] = nOne
text[vIndex] = v+'.'
text[nTwoIndex] = nTwo

#Join text back to a readable passage and print to screen.
newText = " ".join(text)
print('\n\n')
print(newText)

#Print the new text to file.
file = open('Mad Libs.txt', 'a')
file.write('\n\n' + newText)
file.close()

Thank you,

Stuart

I took your code and commented where I would change something:

Replace the words ADJECTIVE, NOUN, ADVERB and NOUN in a passage of text.
Use prompts to input the words to be used.
Print to sceen and save to file.

#Prompt user for the words to add
from pathlib import Path

file = open('Mad Libs.txt', 'w')
file.write('The ADJECTIVE panda walked to the NOUNONE and then VERB. A nearby NOUNTWO was unaffected 
by these events.')
file.close()

# Use sensible variable names
print('Enter an adjective: ')
adjective = input()
print('Enter a noun: ')
noun1 = input()
print('Enter a verb: ')
verb = input()
print('Enter a noun: ')
noun2 = input()

# Use "with" or close the file afterwards, mention the mode to open
with open('Mad Libs.txt', 'r') as file:
    text = file.read()
# or
file = open('Mad Libs.txt', 'r')
text = file.read()
file.close()

# Use a Dictionary for the Replacements and the .replace method
replacements = {
'ADJECTIVE':adjective
'NOUNONE':noun1
'VERB':verb
'NOUNTWO':noun2
}

for a, b in replacements.items():
    text = text.replace(a, b)

# Print to screen.
print('\n\n')
print(text)

#Print the new text to file.
file = open('Mad Libs.txt', 'a')
file.write('\n\n' + text)
file.close()

Think about using "with" to open files, it is much more comfortable. Enjoy the book, i liked it too!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM