Python 类型错误:预期的字符串或缓冲区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16193521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: expected string or buffer
提问by user2290969
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?
任何人都可以帮忙吗?
回答by timss
linesis a list. re.findall()doesn't take lists.
lines是一个列表。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). I.e. readlines()is for loops/iterating:
从help(file.readlines). 即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']
回答by mata
linesis a list of strings, re.findalldoesn't work with that. try:
lines是一个字符串列表,re.findall不适用于它。尝试:
import re, sys
f = open('findallEX.txt', 'r')
lines = f.read()
match = re.findall('[A-Z]+', lines)
print match
回答by thegrinner
readlines()will return a list of all the lines in the file, so linesis a list. You probably want something like this:
readlines()将返回文件中所有行的列表,列表也是如此lines。你可能想要这样的东西:
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
回答by saikrishna jampuram
'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
这可能会帮助你
回答by Sumanyu Nandwani
re.findall finds all the occurrence of the regex in a string and return in a list. Here, you are using a list of strings, you need this to use re.findall
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))

