I have a text file that reads
I
BLESS
THE
RAINS
DOWN
IN
AFRICA
and I need it to be transformed where all the first letters are capitalized and the rest lowercase. The second part is I need this transformed text to be written into a new text document.
text_file = open('lyrics.txt','r')
You can use the title
method on the contents of the file
with open("lyrics.txt") as f:
s = f.read().title()
with open("lyrics.txt", "w") as f:
f.write(s)
You could fit this all into one context manager, but I find the above more readable than
with open("lyrics.txt", "r+") as f:
s = f.read().title()
f.seek(0)
f.write(s)
Is this what you want?:
with open('file.txt','r') as f:
newl = []
l = f.readlines()
for i in l:
newl.append(str(i[0]+i[1:].lower()).strip())
with open('new.txt','w') as f2:
for i in newl:
f2.write(i+'\n')
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.