Parseltongue
Parseltongue

Reputation: 11657

Delete newline / return carriage in file output

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: I know how to manipulate strings to delete returns. I want to physically edit the file so that those returns are deleted.

I'm looking for something like this:

wfile = open("wordlist.txt", "r+")           
for line in wfile:
    if len(line) == 0:
        # note, the following is not real... this is what I'm aiming to achieve.
        wfile.delete(line)

Upvotes: 15

Views: 89960

Answers (7)

Dulangi_Kanchana
Dulangi_Kanchana

Reputation: 1233

This is also a possible solution

file1 = open('myfile.txt','r')
conv_file = open("numfile.txt","w")

temp = file1.read().splitlines()

for element in temp:
    conv_file.write(element)

file1.close()
conv_file.close()

Upvotes: 0

eatkimchi
eatkimchi

Reputation: 59

'whatever\r\r\r\r\r\r\r\r\n\n\n\n\n'.translate(None, '\r\n')

returns

'whatever'

Upvotes: 0

aqua
aqua

Reputation: 3375

>>> string = "testing\n"
>>> string
'testing\n'
>>> string = string[:-1]
>>> string
'testing'

This basically says "chop off the last thing in the string" The : is the "slice" operator. It would be a good idea to read up on how it works as it is very useful.

EDIT

I just read your updated question. I think I understand now. You have a file, like this:

aqua:test$ cat wordlist.txt 
Testing

This

Wordlist

With

Returns

Between

Lines

and you want to get rid of the empty lines. Instead of modifying the file while you're reading from it, create a new file that you can write the non-empty lines from the old file into, like so:

# script    
rf = open("wordlist.txt")
wf = open("newwordlist.txt","w")
for line in rf:
    newline = line.rstrip('\r\n')
    wf.write(newline)
    wf.write('\n')  # remove to leave out line breaks
rf.close()
wf.close()

You should get:

aqua:test$ cat newwordlist.txt 
Testing
This
Wordlist
With
Returns
Between
Lines

If you want something like

TestingThisWordlistWithReturnsBetweenLines

just comment out

wf.write('\n')

Upvotes: 24

Senthil Kumaran
Senthil Kumaran

Reputation: 56851

You can use a string's rstrip method to remove the newline characters from a string.

>>> 'something\n'.rstrip('\r\n')
>>> 'something'

Upvotes: 20

Venu Murthy
Venu Murthy

Reputation: 2134

simply use, it solves the issue.

   string.strip("\r\n")

Upvotes: 2

jfs
jfs

Reputation: 414235

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.

Upvotes: 1

Jakob Bowyer
Jakob Bowyer

Reputation: 34698

The most efficient is to not specify a strip value

'\nsomething\n'.split() will strip all special characters and whitespace from the string

Upvotes: 3

Related Questions