[英]TypeError: expected string or buffer
I have this simple code:我有这个简单的代码:
import re, sys
f = open('findallEX.txt', 'r')
lines = f.readlines()
match = re.findall('[A-Z]+', lines)
print match
I don't know why I am getting the error:我不知道为什么我收到错误:
'expected string or buffer'
'预期的字符串或缓冲区'
Can anyone help?任何人都可以帮忙吗?
lines
is a list. lines
是一个列表。 re.findall()
doesn't take lists. re.findall()
不接受列表。
>>> 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'>
From help(file.readlines)
.来自
help(file.readlines)
。 Ie readlines()
is for loops/iterating:即
readlines()
用于循环/迭代:
readlines(...)
readlines([size]) -> list of strings, each a line from the file.
To find all uppercase characters in your 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']
lines
is a list of strings, re.findall
doesn't work with that. lines
是一个字符串列表, re.findall
不适用于它。 try:尝试:
import re, sys
f = open('findallEX.txt', 'r')
lines = f.read()
match = re.findall('[A-Z]+', lines)
print match
readlines()
will return a list of all the lines in the file, so lines
is a list. readlines()
将返回文件中所有lines
的列表,所以lines
是一个列表。 You probably want something like this:你可能想要这样的东西:
for line in f.readlines(): # Iterates through every line and looks for a match
#or
#for line in f:
match = re.findall('[A-Z]+', line)
print match
Or, if the file isn't too large you can grab it as as single string:或者,如果文件不是太大,您可以将其作为单个字符串抓取:
lines = f.read() # Warning: reads the FULL FILE into memory. This can be bad.
match = re.findall('[A-Z]+', lines)
print match
'lines' term from your snippet consists of set of strings.片段中的“行”术语由一组字符串组成。
lines = f.readlines()
match = re.findall('[A-Z]+', lines)
You cannot send entire lines into the re.findall('pattern',<string>)
您不能将整行发送到
re.findall('pattern',<string>)
You can try to send line by line您可以尝试逐行发送
for i in lines:
match = re.findall('[A-Z]+', i)
print match
or to convert the entire lines collection into single line (each line seperated by space)或将整个行集合转换为单行(每行以空格分隔)
NEW_LIST=' '.join(lines)
match=re.findall('[A-Z]+' ,NEW_LIST)
print match
This might help you这可能会帮助你
re.findall finds all the occurrence of the regex in a string and return in a list. re.findall 在字符串中查找所有出现的正则表达式,并在列表中返回。 Here, you are using a list of strings, you need this to use re.findall
在这里,您使用的是字符串列表,您需要使用它来使用 re.findall
Note - If the regex fails, an empty list is returned.注 - 如果正则表达式失败,则返回一个空列表。
import re, sys
f = open('picklee', 'r')
lines = f.readlines()
regex = re.compile(r'[A-Z]+')
for line in lines:
print (re.findall(regex, line))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.