我创建了一些代码来标识数据集中的特定日期。但是,当尝试引用该日期并将其与包含相关数据的其他日期进行比较时,我似乎无法返回匹配项。下列日期是一个列表,但type(xyz)
返回class: 'list'
...
查找日期
for item in soup.find('td', text='document period end date').parent.find_all('td', {'class':['text']}):
if len(item.text.strip()) > 0:
report_date = [item.text.strip()]
print(report_date) # class: 'list'
返回:
['mar. 31, 2018']
现在让我们尝试查找该日期的匹配项。这是代码:
for filename in filenames:
print('-----------------------------')
i = 1
for item in soup.select('filename:contains("' + filename + '")'):
if filename in item.text.strip():
for th in item.find_all('th', {'class':['th']}):
headers = [th.text.strip()]
print(headers) # class: 'list'
if headers == report_date:
print('match')
else:
print('no match')
返回:
['mar. 31, 2018']
no match
['sep. 30, 2017']
no match
['6 months ended']
no match
['mar. 31, 2018']
no match
['6 months ended']
no match
['mar. 31, 2018']
no match
尝试collections.Counter():
if collections.Counter(headers) == collections.Counter(report_date):
print('match')
else:
print('no match')
没有骰子:
['mar. 31, 2018']
no match
['sep. 30, 2017']
no match
['6 months ended']
no match
['mar. 31, 2018']
no match
['6 months ended']
no match
['mar. 31, 2018']
no match
尝试.sort()就像下面的一些链接所示:
if headers.sort() == report_date.sort():
print('match')
else:
print('no match')
现在所有都匹配..?:
['mar. 31, 2018']
match
['sep. 30, 2017']
match
['6 months ended']
match
['mar. 31, 2018']
match
['6 months ended']
match
['mar. 31, 2018']
match
在StackOverflow问题identical string returning false或Python '==' incorrectly returning false或简单的Google搜索主题中,我都找不到合适或有用的答案。