【问题标题】:Split text string from a certain percentage从一定百分比拆分文本字符串
【发布时间】:2022-01-25 17:39:14
【问题描述】:

我需要从某个分数中拆分文本。例如 50% 文本后的第一个点。 示例:

a = 'Hello this text. It is a reference. But I like it very much, I would like to share with you everything I know. Goodbye friends.'

如果我想在 50% 之后将文本从第一个点开始,我会得到:

'Goodbye friends.'

我怎样才能得到这个结果?

【问题讨论】:

  • 有问题吗?
  • '再见朋友。'如果是什么的 50%?

标签: python text split


【解决方案1】:
a = 'Hello this text. It is a reference. But I like it very much, I would like to share with you everything I know. Goodbye friends.'

#first we get the index of the string to the half, using len function to get the total len,
#then that len is divided by 2, and using :len(a) we take the 
#the last place of the string
b = a[int(len(a) / 2):len(a)]

#with find function save in a variable the index of the first dot
#in the string saved on variable b
c = b.find(".")

#In a new variable taken the new string, using square 
#brackets we take the index of the first dot saved on variable c, and then using len 
#function we indicate the last place of the string 
d = b[c:len(b)]

print(d)

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
  • 完美,我也一直这么想。
  • 希望对答案有一些解释!
猜你喜欢
  • 1970-01-01
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多