TypeError: expected string or bytes-like object in re
TypeError: expected string or bytes-like object in re
我想从这个巨大的列表中获取 'Asia/Tel_Aviv' 和 'Asia/Jeruslam',这是完整的代码:
import pytz
alltmzn=pytz.alltimezones
print(alltmzn)
t=alltmzn[234:332]
for i in re.findall("^J|Tel",t):
print(i)
错误:
TypeError Traceback (most
recent call last)
<ipython-input-133-fe8a33f8999a> in <module>
1 t=alltmzn[234:332]
----> 2 for i in re.findall("^J|Tel",t):
3 print(i)
C:\ProgramData\Anaconda3\lib\re.py in findall(pattern, string, flags)
239
240 Empty matches are included in the result."""
--> 241 return _compile(pattern, flags).findall(string)
242
243 def finditer(pattern, string, flags=0):
TypeError: expected string or bytes-like object`
t
是一个列表,而 findall
需要一个字符串。
你可以这样改写:
for tz in t:
if re.search(pattern, tz):
print(tz)
请注意,您的模式将匹配字符串 starting 与“Tel”或“Jel”(不是 Jer,并且不包括之前的“Asia/”),这可能不是什么你要。
我想从这个巨大的列表中获取 'Asia/Tel_Aviv' 和 'Asia/Jeruslam',这是完整的代码:
import pytz
alltmzn=pytz.alltimezones
print(alltmzn)
t=alltmzn[234:332]
for i in re.findall("^J|Tel",t):
print(i)
错误:
TypeError Traceback (most
recent call last)
<ipython-input-133-fe8a33f8999a> in <module>
1 t=alltmzn[234:332]
----> 2 for i in re.findall("^J|Tel",t):
3 print(i)
C:\ProgramData\Anaconda3\lib\re.py in findall(pattern, string, flags)
239
240 Empty matches are included in the result."""
--> 241 return _compile(pattern, flags).findall(string)
242
243 def finditer(pattern, string, flags=0):
TypeError: expected string or bytes-like object`
t
是一个列表,而 findall
需要一个字符串。
你可以这样改写:
for tz in t:
if re.search(pattern, tz):
print(tz)
请注意,您的模式将匹配字符串 starting 与“Tel”或“Jel”(不是 Jer,并且不包括之前的“Asia/”),这可能不是什么你要。