我有一个嵌套列表,我想在其中进行一些字符串操作。但是,我遇到了此类错误。有人知道这是什么意思吗?
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
答案 0 :(得分:0)
有两件事。
首先, <link type="text/css" href="newWebsiteFinalCSS.css" rel="stylesheet">
需要一个字符串,当前您正在提供列表。如果只有一个元素,为什么需要包括一个列表?如果还有更多的只是将它们弄平?
第二个re.search
方法不返回附加列表,而是返回.append
。这是一个就地操作,因此只需打印列表即可。
None
输出:
import re
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:]
cor_welldata.append(well2)
print(cor_welldata)
format_Well_Data(['AMV-10st102/13/19 4 954 3,968 ',
'AMV-0201/24/19 6 3,078 1,303 ' ])
答案 1 :(得分:0)
您需要从函数返回数据,再在输入中,将数据保留在list列表中。以下是无需修改输入数据的解决方案
import re
def format_Well_Data(welldata):
cor_welldata=[]
for Well in welldata:
for wells in Well:
pattern = re.search(r'\d{2}\/',wells)
ii = pattern.start()
well2 = wells[0:ii] + ' ' + wells[ii:]
cor_welldata.append(well2)
print(cor_welldata)
return cor_welldata
format_Well_Data([['AMV-10st102/13/19 4 954 3,968 '],
['AMV-0201/24/19 6 3,078 1,303 '] ])
否则,如果您不修改当前代码,则可以将输入数据修改为
来自
[['AMV-10st102/13/19 4 954 3,968 '],['AMV-0201/24/19 6 3,078 1,303 '] ]
到['AMV-10st102/13/19 4 954 3,968 ','AMV-0201/24/19 6 3,078 1,303 ' ]
这是不完善的代码
import re
def format_Well_Data(welldata):
cor_welldata=[]
for Well in welldata:
patt = re.findall(r'\d\d/\d\d/\d\d',Well)
str1 = Well.replace(patt[0], ' {}'.format(patt[0]))
cor_welldata.append(str1)
return cor_welldata
format_Well_Data(['AMV-10st102/13/19 4 954 3,968 ',
'AMV-0201/24/19 6 3,078 1,303 ' ])
"""
output
['AMV-10st1 02/13/19 4 954 3,968 ', 'AMV-02 01/24/19 6 3,078 1,303 ']
"""