Removing all spaces in text file with Python 3.x

前端 未结 3 457
日久生厌
日久生厌 2021-01-14 12:33

So I have this crazy long text file made by my crawler and it for some reason added some spaces inbetween the links, like this:

https://example.com/asdf.html         


        
3条回答
  •  抹茶落季
    2021-01-14 13:15

    Read text from file, remove spaces, write text to file:

    with open('file.txt', 'r') as f:
        txt = f.read().replace(' ', '')
    
    with open('file.txt', 'w') as f:
        f.write(txt)
    

    In @Leonardo Chirivì's solution it's unnecessary to create a list to store file contents when a string is sufficient and more memory efficient. The .replace(' ', '') operation is only called once on the string, which is more efficient than iterating through a list performing replace for each line individually.

    To avoid opening the file twice:

    with open('file.txt', 'r+') as f:
        txt = f.read().replace(' ', '')
        f.seek(0)
        f.write(txt)
        f.truncate()
    

    It would be more efficient to only open the file once. This requires moving the file pointer back to the start of the file after reading, as well as truncating any possibly remaining content left over after you write back to the file. A drawback to this solution however is that is not as easily readable.

提交回复
热议问题