简体   繁体   中英

Python: Replacing character in for loop

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

text = "Rmyname.lastname@mail.com"
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?

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('@') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM