Delete newline / return carriage in file output

后端 未结 6 1517
无人及你
无人及你 2020-12-16 16:55

I have a wordlist that contains returns to separate each new letter. Is there a way to programatically delete each of these returns using file I/O in Python?

Edit

6条回答
  •  抹茶落季
    2020-12-16 17:39

    Remove empty lines in the file:

    #!/usr/bin/env python
    import fileinput
    
    for line in fileinput.input("wordlist.txt", inplace=True):
        if line != '\n':
           print line,
    

    The file is moved to a backup file and standard output is directed to the input file.

提交回复
热议问题