Python:替换for循环中的字符

2024-09-27 00:12:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我要删除“.”字符。当我用这个的时候

text = "Rmyname.lastname@mail.com"
text = (text.replace('.',' '))
head, sep, tail = text.partition('@')
print(head)

它工作,这是输出:Rmyname lastname

但是当加载一个外部文件并读取每一行时,它不会替换“.”字符

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)

这是输出:Rmyname.lastname

我怎样才能解决这个问题


Tags: csvfiletextcommail字符headreplacesep

热门问题