Removing all spaces in text file with Python 3.x

前端 未结 3 456
日久生厌
日久生厌 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:11

    You can open file and read line by line and remove white space -

    Python 3.x:

    with open('filename') as f:
        for line in f:
            print(line.strip())
    

    Python 2.x:

    with open('filename') as f:
        for line in f:
            print line.strip()
    

    It will remove space from each line and print it.

    Hope it helps!

    0 讨论(0)
  • 2021-01-14 13:12

    Seems like you can't directly edit a python file, so here is my suggestion:

    # first get all lines from file
    with open('file.txt', 'r') as f:
        lines = f.readlines()
    
    # remove spaces
    lines = [line.replace(' ', '') for line in lines]
    
    # finally, write lines in the file
    with open('file.txt', 'w') as f:
        f.writelines(lines)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题