lines
è un elenco. re.findall()
non accetta elenchi.
>>> import re
>>> f = open('README.md', 'r')
>>> lines = f.readlines()
>>> match = re.findall('[A-Z]+', lines)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
>>> type(lines)
<type 'list'>
Da help(file.readlines)
. Cioè readlines()
è per cicli/iterazione:
readlines(...)
readlines([size]) -> list of strings, each a line from the file.
per trovare tutti i caratteri maiuscoli nel file:
>>> import re
>>> re.findall('[A-Z]+', open('README.md', 'r').read())
['S', 'E', 'A', 'P', 'S', 'I', 'R', 'C', 'I', 'A', 'P', 'O', 'G', 'P', 'P', 'T', 'V', 'W', 'V', 'D', 'A', 'L', 'U', 'O', 'I', 'L', 'P', 'A', 'D', 'V', 'S', 'M', 'S', 'L', 'I', 'D', 'V', 'S', 'M', 'A', 'P', 'T', 'P', 'Y', 'C', 'M', 'V', 'Y', 'C', 'M', 'R', 'R', 'B', 'P', 'M', 'L', 'F', 'D', 'W', 'V', 'C', 'X', 'S']
Sostituisci 'f.readlines()' di 'f.read()'. –
Se le righe fossero 'None' si otterrebbe lo stesso errore qui se si inserisce un elenco. Ciò si verifica anche con 're.sub' nella stessa circostanza. Quindi è un errore TypeE (il tipo errato inserito). Ho appena detto questo perché ho cercato ciò che ha causato questo errore e trovato il tuo post (e ho avuto un Nonetype in caso di incidente). – Shule