Remove specific lines from a large text file in python

前端 未结 3 1974
情歌与酒
情歌与酒 2020-12-17 07:01

I have several large text text files that all have the same structure and I want to delete the first 3 lines and then remove illegal characters from the 4th line. I don\'t w

3条回答
  •  时光取名叫无心
    2020-12-17 07:54

    Just try it for each file. 100 MB per file is not that big, and as you can see, the code to just make an attempt is not time-consuming to write.

    with open('file.txt') as f:
      lines = f.readlines()
    lines[:] = lines[3:]
    lines[0] = lines[0].replace('Rx(db)', 'Rx')
    lines[0] = lines[0].replace('Best Unit', 'Best_Unit')
    with open('output.txt', 'w') as f:
      f.write('\n'.join(lines))
    

提交回复
热议问题