【发布时间】:2019-05-12 22:21:38
【问题描述】:
我有一个嵌套列表,我想在其中进行一些字符串操作。但是,我遇到了这种类型的错误。有谁知道这是什么意思?
def format_Well_Data(welldata):
cor_welldata=[]
for Well in welldata:
pattern = re.search(r'\d{2}\/',Well)
ii = pattern.start()
well2 = Well[0:ii] + ' ' + Well[ii:]
a=cor_welldata.append(well2)
print(a)
format_Well_Data([['AMV-10st102/13/19 4 954 3,968 '],
['AMV-0201/24/19 6 3,078 1,303 ' ]])
The code should add 2 space at the start of the date(mm/dd/yy):
[['AMV-10st1 02/13/19 4 954 3,968 '],['AMV-02 01/24/19 6 3,078 1,303 ' ]]
However, i get this huge error message:
C:\Users\gogut\AppData\Local\Continuum\anaconda3\envs\PTT\python.exe C:/APPL/DPI/PDF_reader/regextest_loop.py
Traceback (most recent call last):
File "C:/APPL/DPI/PDF_reader/regextest_loop.py", line 14, in <module>
['AMV-0201/24/19 6 3,078 1,303 ' ]])
File "C:/APPL/DPI/PDF_reader/regextest_loop.py", line 6, in format_Well_Data
pattern = re.search(r'\d{2}\/',Well)
File "C:\Users\gogut\AppData\Local\Continuum\anaconda3\envs\PTT\lib\re.py", line 182, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object
【问题讨论】:
-
看来 welldata 是一个列表,因此你的 for 循环中的 Well 也将是一个列表;但是,您只能在字符串上使用正则表达式进行搜索。也许您想将两个字符串放在一个列表中,而不是将它们单独放在两个列表中?
标签: python list loops nested-lists