栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

预期的字符串或缓冲区(在re.sub中)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

预期的字符串或缓冲区(在re.sub中)

当您有多个匹配组时,

re.findall
返回
list
n-
tuple
s:

re.findall('(foo).(bar)', 'foo foo bar foo|bar')Out[5]: [('foo', 'bar'), ('foo', 'bar')]

因此,很明显每个

entry
in
tuples
是一个
tuple
。当您将传递
tuple
re.sub
时,它会抱怨。

tuples = re.findall('(foo).(bar)', 'foo foo bar foo|bar')for entry in tuples:    re.sub('oo','ox',entry).../usr/lib/python3.3/re.py in sub(pattern, repl, string, count, flags)    168     a callable, it's passed the match object and must return    169     a replacement string to be used."""--> 170     return _compile(pattern, flags).sub(repl, string, count)    171     172 def subn(pattern, repl, string, count=0, flags=0):TypeError: expected string or buffer

因此,做其他事情。也许使用

map

for entry in tuples:    print(' '.join(map(lambda s: re.sub('oo','ox',s),entry)))fox barfox bar

或者更容易理解

writer.writerow([re.sub(r'W', " ",s) for s in entry])

等等



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659822.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号