Using python, write code for problem. The text file ShortColors.txt is used and i have put screenshot of data from the text file. Please also put screenshot of your code.
Expert Answer
#!/usr/bin/env python3
shortColors=open(“ShortColors.txt”,”r”)
colors=shortColors.readlines()
shortColors.close()
shortColors=open(“ShortColors.txt”,”w”)
for color in colors:
#remove character n from color
color=color.strip(‘n’)
#if length of color <= 6 write back to file
if (len(color) <= 6):
shortColors.write(color+”n”)
shortColors.close()
print(“ShortColors.txt has successfully modified!”)
Indentation:
Ouput:
ShortColors.txt before
Languid lavender Medium spring bud Raspberry pink Ecru Mulberry Brown (web) Mikado yellow Cerulean blue Deep carmine pink Dark olive green Pale robin egg blue Purple taupe Rose bonbon Persian plum Ferrari Red Pink pearl Magic mint Scarlet Medium blue Tangerine yellow Capri Blue-violet Dark champagne Tiffany Blue Fandango Old Lace Light coral Brilliant lavender Fawn Maroon (HTML/CSS) Medium carmine Trolley Grey Electric ultramarine Smalt (Dark powder blue) Maroon (X11) Jonquil Lemon Pear Persian rose Persian pink Harlequin Light sea green Carnation pink Seal brown Deep sky blue Pansy purple Piggy pink Sea green Moss green Smokey topaz Ultramarine Royal purple Rose taupe Phthalo blue Citrine Khaki (X11) (Light khaki) Dark magenta Deep cerise Rifle green Red-violet Sandstorm Pistachio Cambridge Blue Pink Sherbert Denim Medium taupe Arsenic Electric indigo Viridian June bud Saffron Linen Moonstone blue Periwinkle Saddle brown Deep saffron Lavender blush Peach-orange Tea rose (rose) Forest green (web) Pale copper Plum (web) Cool grey Heliotrope Turquoise Orange peel Palatinate purple Dark khaki Lava La Salle Green Bright ube Dark candy apple red Mint Lime (color wheel) Bright pink Medium champagne Light Thulian pink Blue-green Antique fuchsia Ghost white
ShortColor.txt after
Ecru
Capri
Fawn
Lemon
Pear
Denim
Linen
Lava
Mint
Note: I have assumed that color will be on each line in the file. I have used a different color list for testing, but you can use your list.