Alex
Alex

Reputation: 1934

remove \n from the end of list item

I have this code

def return_not_existing_signals(list_of_signals,read_or_write) :
    not_used_produced_signals = []
    #print(list_of_signals)
    if read_or_write =="read":
        file_to_create= "c:\\path\\read.txt"
    else:
        file_to_create = "c:\\path\\write.txt"

    for i in list_of_signals :
        i.replace('\n','')
        if not search_for_signals(i) :
            not_used_produced_signals.append(i)
    write_to_txt(not_used_produced_signals,file_to_create)


def search_for_signals(name_of_signal):
    for filename in Path('c:\path').glob('**/*.h') : 
        with open(filename) as f:
            if name_of_signal in f.read():
                return True
    return False

The problem is that some of the list_of_signals are having \n at the end ( example: test1234rwq4\n)

I.replace(\'n',' ' ) isn't working

Upvotes: 0

Views: 74

Answers (2)

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

from docs:

string.replace(s, old, new[, maxreplace])

Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.

Hence you need to to it as,

i = i.replace('\n','')

Upvotes: 3

Kirikkayis
Kirikkayis

Reputation: 83

strg = "Helllo\nwhatsup?"
New_strg = strg.replace('\n','')
print(New_strg)

Output:

Hellowhatsup?

Upvotes: 1

Related Questions