python替换文件中的变量_Python – 使用beautifulSoup查找文本,然后替换原始汤变量...

你只能通过.replace()做你想做的事.从

BeautifulSoup documentation on NavigableString:

You can’t edit a string in place, but you can replace one string with another, using 07001.

这正是你需要做的;获取每个匹配项,然后在包含的文本上调用.replace()并将原始文本替换为:

findtoure = commentary.find_all(text = re.compile('Gnegneri Toure Yaya'))

for comment in findtoure:

fixed_text = unicode(comment).replace('Gnegneri Toure Yaya', 'Yaya Toure')

comment.replace_with(fixed_text)

如果您想进一步使用这些评论,您需要进行新的查找:

findtoure = commentary.find(text = re.compile('Yaya Toure'))

或者,如果你需要的只是生成的unicode文本(所以没有连接NavigableString对象),只需收集fixed_text对象:

findtoure = commentary.find_all(text = re.compile('Gnegneri Toure Yaya'))

fixed_comments = []

for comment in findtoure:

fixed_text = unicode(comment).replace('Gnegneri Toure Yaya', 'Yaya Toure')

comment.replace_with(fixed_text)

fixed_comments.append(fixed_text)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值