removing tag from a line in Python

后端 未结 3 1852
感动是毒
感动是毒 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:47

    That's because each line ends with newline character(s).

    You can fix it like this (and automatically close the file):

    def main():
        with open("test.txt", "r") as fileR:
            for line in (line.rstrip() for line in fileR):
                if line.endswith('
    '): line = line[:-6] print line

提交回复
热议问题