Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用re.findall()查找从A到Z的所有字母时出错_Python_Regex_Python 3.x - Fatal编程技术网

Python 使用re.findall()查找从A到Z的所有字母时出错

Python 使用re.findall()查找从A到Z的所有字母时出错,python,regex,python-3.x,Python,Regex,Python 3.x,我的代码是: import re key, val = ' ', 2 self._l[key] = re.findall(r"[A-Z]+", val) 错误: return _compile(pattern, flags).findall(string) TypeError: expected string or bytes-like object 那是因为你问题中的val是int。如果将其设置为str对象,它将工作: >>> val = 2 >>>

我的代码是:

import re
key, val = '  ', 2
  self._l[key] = re.findall(r"[A-Z]+", val)
错误:

return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object

那是因为你问题中的
val
int
。如果将其设置为
str
对象,它将工作:

>>> val = 2
>>> type(val)
<type 'int'>

>>> val = "2"
>>> type(val)
<type 'str'>
>>> re.findall(r"[A-Z]+", val)
[]

>>> val = 'SOME 123 STRING'
>>> re.findall(r"[A-Z]+", val)
['SOME', 'STRING']
>>val=2
>>>类型(val)
>>>val=“2”
>>>类型(val)
>>>关于findall(r“[A-Z]+”,val)
[]
>>>val='SOME 123 STRING'
>>>关于findall(r“[A-Z]+”,val)
['SOME','STRING']

…那么错误消息的哪一部分不清楚?对不起,我是编程新手。一个小小的改变就起了作用。