Lis*_*adk 3 python string split find
如何按单词的位置分割字符串?
我的数据如下所示:
test = 'annamarypeterson, Guest Relations Manager, responded to this reviewResponded 1 week agoDear LoreLoreLore,Greetings from Amsterdam!We have received your wonderful comments and wanted to thank you for sharing your positive experience with us. We are so thankful that you have selected the Andaz Amsterdam for your special all-girls weekend getaway. Please come and see us again in the near future and let our team pamper you and your girlfriends!!Thanks again!Anna MaryAndaz Amsterdam -Guest RelationsReport response as inappropriateThank you. We appreciate your input.This response is the subjective opinion of the management representative'
Run Code Online (Sandbox Code Playgroud)
我需要以下输出:
responder = 'annamarypeterson, Guest relations Manager'
date = 'Responded 1 week ago'
response = 'Dear ....' #without 'This response is the subjective opinion of the management representative'
Run Code Online (Sandbox Code Playgroud)
我知道find.()函数给出了单词的位置,并且我想用这个位置来告诉Python在哪里分割它。例如:
splitat = test.find('ago')+3
Run Code Online (Sandbox Code Playgroud)
我可以使用什么函数进行整数分割?该split()函数不适用于int。
您可以使用切片对字符串(和列表)执行此操作:
str = "hello world!"
splitat = 4
l, r = str[:splitat], str[splitat:]
Run Code Online (Sandbox Code Playgroud)
将导致:
>>> l
hell
>>> r
o world!
Run Code Online (Sandbox Code Playgroud)
也许最简单的解决方案是使用字符串切片:
test = 'annamarypeterson, Guest Relations Manager, responded to this reviewResponded 1 week agoDear LoreLoreLore,Greetings from Amsterdam!We have received your wonderful comments and wanted to thank you for sharing your positive experience with us. We are so thankful that you have selected the Andaz Amsterdam for your special all-girls weekend getaway. Please come and see us again in the near future and let our team pamper you and your girlfriends!!Thanks again!Anna MaryAndaz Amsterdam -Guest RelationsReport response as inappropriateThank you. We appreciate your input.This response is the subjective opinion of the management representative'
pos = test.find('ago') + 3
print(test[:pos], test[pos:])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15507 次 |
| 最近记录: |