removing tag from a line in Python

后端 未结 3 1821
感动是毒
感动是毒 2021-01-28 02:41

I have a text that has the following schema:

word1:word2
word3:word4
...

I would like to remove the last part
, a

3条回答
  •  孤独总比滥情好
    2021-01-28 02:56

    I'd recommend using a regex replace for this instead of what you're currently using.

    import re
    
    def main():
      fileR=open('test.txt','r')
      for line in fileR:
        line = re.replace(r'
    $','',line) print line

    Or, if you want, you can just replace all of them at once before printing out each line individually because python's regex is global by default.

    import re
    
    def main():
      fileR=open('test.txt','r')
      fileR = re.replace(r'
    $','',fileR) for line in fileR: print line

提交回复
热议问题