【问题标题】:Python 2.7, print the list output without quotes, extracting output from htmlPython 2.7,打印不带引号的列表输出,从html中提取输出
【发布时间】:2018-04-09 19:51:49
【问题描述】:

我希望输出类似于周一下午 5:00 到凌晨 12:00。删除输出检查中的所有单引号和空格:代码如下

for count in glob.glob(os.path.join("C:\\Users\\test", "*.html")):
    soup=BeautifulSoup(open(files), 'html.parser')
    hours=soup.find_all( 'table', {'class' : "table"
    [0].get_text().strip().split()
    check=[i.encode('utf-8').strip().replace("-","to" ) for i in hours]
    print check

当前输出:

['Mon', '5:00', 'pm', 'to', '12:00', 'am', 'Tue', '5:00', 'pm', 'to', '12:00', 'am']

【问题讨论】:

  • 当前,它将输出打印为 ['Mon', '5:00', 'pm', 'to', '12:00', 'am', 'Tue', '5 :00', 'pm', 'to', '12:00', 'am']
  • ['Mon', '5:00', 'pm', 'to', '12:00', 'am', 'Tue', '5:00', 'pm' , 'to', '12:00', 'am'

标签: python-2.7 list beautifulsoup


【解决方案1】:

如果输出一致,可以加入输出列表得到想要的结果:

output = ['Mon', '5:00', 'pm', 'to', '12:00', 'am', 'Tue', '5:00', 'pm', 'to', '12:00', 'am']
date_list = []

# iterate over list (assuming list is consistent - with 6 values per section
for i in range(0, len(output), 6):
    # join the values on the list and append to date list
    date_list.append(' '.join(output[i:i+6]))

#print results
print(', '.join(date_list))

我的输出:

Mon 5:00 pm to 12:00 am, Tue 5:00 pm to 12:00 am

【讨论】:

  • 所有输出都应该在一行中,周一下午 5:00 到凌晨 12:00,周二下午 5:00 到凌晨 12:00,以此类推直到周日
【解决方案2】:

这就是我的做法...不过可能有更好的方法。

check=str(check)
check=check.strip('[]')
check=check.strip("''")
check=check.replace("','","")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2019-10-11
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多