johnjohneusee
johnjohneusee

Reputation: 3

Python: Replacing character in for loop

I want to remove the "." character. When I use this

text = "[email protected]"
text = (text.replace('.',' '))
head, sep, tail = text.partition('@')
print(head)

It works and this is the output: Rmyname lastname

But when load an external file and read every line, its doesnt replace the "." character.

with open('found.txt', 'r') as csvfile:
    spamreader = csv.reader(csvfile)
    for row in spamreader:
        head = (row[0].replace('.', ' '))

        head, sep, tail = row[0].partition('@')
        print(head)

This is the output: Rmyname.lastname

How can i solve this?

Upvotes: 0

Views: 49

Answers (1)

DYZ
DYZ

Reputation: 57033

You store the result of the replacement into the variable head. The original row[0] still has the period. Change row[0].partition('@') to head.partition('@').

Upvotes: 2

Related Questions