我也看到了类似的问题(在这里,在这里)之前,SO,我知道这re.sub需要一个字符串(其中,我相信,我提供的),但我不知道什么是错在下面的代码:
tuples = re.findall(r'id":"(.*?)".*?name":"(.*?)"', response.text, re.DOTALL)
outfile = open("badEXtsWithIDs.csv", "wb")
print "Writing into CSV"
writer = csv.writer(outfile)
for entry in tuples:
writeName = re.sub(r'\W', " ", entry)
writer.writerow(writeName)
Run Code Online (Sandbox Code Playgroud)
我认为这re.sub需要一个str变量,但是,不是进入str吗?我收到一个错误:TypeError: expected string or buffer就行了re.sub.任何帮助赞赏.
当您有多个匹配组时,re.findall返回listn- tuples:
re.findall('(foo).(bar)', 'foo foo bar foo|bar')
Out[5]: [('foo', 'bar'), ('foo', 'bar')]
Run Code Online (Sandbox Code Playgroud)
因此,明确各自entry在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
Run Code Online (Sandbox Code Playgroud)
所以,做点别的.也许用map:
for entry in tuples:
print(' '.join(map(lambda s: re.sub('oo','ox',s),entry)))
fox bar
fox bar
Run Code Online (Sandbox Code Playgroud)
或者更容易理解
writer.writerow([re.sub(r'\W', " ",s) for s in entry])
Run Code Online (Sandbox Code Playgroud)
等等
| 归档时间: |
|
| 查看次数: |
11170 次 |
| 最近记录: |