I have seen similar questions (here, here) before on SO and I know that re.sub
expects a string (which, I believe, I am providing) but I don't know what's wrong in the following code:
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)
I think that re.sub
needs a str
variable but, isn't entry a str
? I get a error: TypeError: expected string or buffer
on the line havingre.sub
. Any help appreciated.
When you have more than one matching group,
re.findall
returns alist
of n-tuple
s:So clearly each
entry
intuples
is atuple
. When you pass atuple
tore.sub
, well, it complains.So, do something else. Maybe use
map
:Or more readably a comprehension
etc.