【问题标题】:how to split the data to get the desired Output in python?如何拆分数据以在 python 中获得所需的输出?
【发布时间】:2020-12-28 15:31:05
【问题描述】:

我有一个输入,我想拆分文本以获得我使用过的所需输出txt.split(":")[-1],但这并没有解决我的问题。需要 Python 编码新手的帮助。

输入:End Date: 10-09-2020 10:00 AM
所需输出:10-09-2020 10:00 AM

【问题讨论】:

  • 如果你想采用你的方法,你可能需要使用 strip() 和 .join() 来获得你想要的输出
  • print (txt.split(":")) - 你为什么使用[-1]?你可以在你的代码中改变什么来得到你想要的?你读过the docs吗?

标签: python string split


【解决方案1】:
txt.split("End Date:")[-1]

解决问题

【讨论】:

    【解决方案2】:

    尝试:

    input = "End Date: 10-09-2020 10:00 AM"
    matcher = "End Date: " # get a constant identifier to look for
    
    # now we want to slice the string in such way that leaves only the part 
    # after the matcher. So we find the first occurence of matcher and then
    # add it's length to see where is the end of the matcher. then we
    # simply take the remaining string
    desired = input[input.find(matcher)+len(matcher):]
    

    【讨论】:

    • @PatrickArtner 正确,使用 input[10:] 也可以
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2013-10-16
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    相关资源
    最近更新 更多