【问题标题】:How can I strip data from the list python如何从列表 python 中删除数据
【发布时间】:2021-01-26 23:46:44
【问题描述】:

[我只想保留年份而不是括号中的数字。][1]

[这是我当前的输出。我想去除括号中的数据,并对列表中存在的所有数据执行此操作,直到结束][2]

[1]https://i.stack.imgur.com/4Q9Wg.png

[2]https://i.stack.imgur.com/SOVQO.png

【问题讨论】:

  • 请将您的代码发布为代码而不是图像
  • 使用split()

标签: python web-scraping beautifulsoup strip


【解决方案1】:

使用.split() 函数。

striped_titles = titles.split(",")

所以,现在您可以访问新列表 striped_titles 中的每个元素

【讨论】:

    【解决方案2】:

    我们可以通过使用正则表达式来做到这一点。

    import re  # Import
    reg = "[\(\[].*?[\)\]]"  # Regex Expression
    stripped_title = [re.sub(reg, "", i) for i in title]
    print(stripped_title)
    
    
    ##2nd Case
    stripped_title = [re.search('\d+', i).group(0) for i in title]
    print(stripped_title)
    

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 2012-07-08
      • 1970-01-01
      • 2019-05-04
      • 2020-11-14
      • 2011-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多