PYHTON将文本内容还原成正常顺序
原文本内容为
.egassem siht htiw yad s’enoemos nethgirb ot ytinutroppo eht no tuo ssim tsuj lliw uoy,uoy ot neppah lliw dab gnihton,yrrow t’nod ,t’nod uoy fi dnA.pihsdneirf rieht etaicerppa uoy taht wonk meht tel ot tnaw uoy ohw esoht ot,nwod yllaer era uoy nehw sgniht fo edis rethgirb eht ees uoy ekam taht esoht ot,ti deen yllaer uoy nehw elims uoy ekam ohw esoht ot,rehtona ro yaw eno ni efil ruoy dehcuot evah ohw esoht ot,uoy ot gnihtemos naem ohw elpoep esoht ot egassem siht dnes esaelP .gniyrc si uoy dnuora enoyreve dna gnilims si ohw eno eht er'uoy,eid uoy nehw taht os efil ruoy eviL.gnilims saw uoy dnuora enoyreve dna gniyrc erew uoy,nrob erew uoy nehW .sehcatraeh dna seruliaf tsap ruoy fo og tel uoy litnuefil ni llew no og t’nac uoy ,tsap nettogrof a no desab eb syawla lliw erutuf tsethgirb ehT.raet a htiw sdne dna ssik a htiw sworg,elims a htiw snigeb evoL.sevil rieht dehcuot evah ohw elpoep fo ecnatropmi eht etaicerppa nac yeht ylno rof,deirt evah ohw esoht dna,dehcraes evah ohw esoht ,truh ohw esoht,yrc ohw esoht rof seil ssenippaH.yaw rieht gnola semoc taht gnihtyreve fo tsom eht ekam tsuj yeht;gnihtyreve fo tseb eht evah ylirassecen t’nod elpoep fo tseippah ehT .oot ,nosrep rehto eht struh ylbaborp ti,uoy struh ti taht leef uoy fI.seohs’srehto ni flesruoy tup syawlA ?yppah uoy ekam ot epoh hguone,namuh uoy peek ot worros hguone,gnorts uoy ekam ot slairt hguone,teews uoy ekam ot ssenippah hguone evah uoy yaM .od ot tnaw uoy sgniht eht lla od ot ecnahc eno dna efil eno ylno evah uoy esuaceb,eb ot tnaw uoy tahw eb;og ot tnaw uoy erehw og;maerd ot tnaw uoy tahw maerD !laer rof meht guh dna smaerd ruoy morf meht kcip ot tnaw tsuj uoy taht hcum os enoemos ssim uoy nehw efil ni stnemom era erehT
这篇文章一看就是将所有字符串反转且字符串中单词也反转了所以要还原成正常顺序就要进行反转
def gettext():
txt=open("input.txt","r",encoding='UTF-8').read()
txt=txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}。~ ‘’':
txt=txt.replace(ch," ")
return txt
rtxt=gettext()
str_dst=rtxt.split()#以空格符为分隔符,将各单词取出来放在列表中
result = ""
for c in str_dst:#拆字
new=c[::-1]#将字符串中的单词反转
result =new+result#将字符串反转
print(result)
反转后的内容为
therearemomentsinlifewhenyoumisssomeonesomuchthatyoujustwanttopickthemfromyourdreamsandhugthemforrealdreamwhatyouwanttodreamgowhereyouwanttogobewhatyouwanttobebecauseyouhaveonlyonelifeandonechancetodoallthethingsyouwanttodomayyouhaveenoughhappinesstomakeyousweetenoughtrialstomakeyoustrongenoughsorrowtokeepyouhumanenoughhopetomakeyouhappyalwaysputyourselfinothersshoesifyoufeelthatithurtsyouitprobablyhurtstheotherpersontoothehappiestofpeopledontnecessarilyhavethebestofeverythingtheyjustmakethemostofeverythingthatcomesalongtheirwayhappinessliesforthosewhocrythosewhohurtthosewhohavesearchedandthosewhohavetriedforonlytheycanappreciatetheimportanceofpeoplewhohavetouchedtheirliveslovebeginswithasmilegrowswithakissandendswithatearthebrightestfuturewillalwaysbebasedonaforgottenpastyoucantgoonwellinlifeuntilyouletgoofyourpastfailuresandheartacheswhenyouwerebornyouwerecryingandeveryonearoundyouwassmilingliveyourlifesothatwhenyoudieyou'retheonewhoissmilingandeveryonearoundyouiscryingpleasesendthismessagetothosepeoplewhomeansomethingtoyoutothosewhohavetouchedyourlifeinonewayoranothertothosewhomakeyousmilewhenyoureallyneedittothosethatmakeyouseethebrightersideofthingswhenyouarereallydowntothosewhoyouwanttoletthemknowthatyouappreciatetheirfriendshipandifyoudontdontworrynothingbadwillhappentoyouyouwilljustmissoutontheopportunitytobrightensomeonesdaywiththismessage