Python:按字符位置拆分字符串

11

我该如何根据单词的位置来拆分字符串?

我的数据长这样:

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'

我需要这个输出结果:
responder = 'annamarypeterson, Guest relations Manager'
date = 'Responded 1 week ago'
response = 'Dear ....' #without 'This response is the subjective opinion of the management representative'

我知道find.()函数给出单词的位置,我想使用这个位置告诉Python在哪里切割它。例如:

splitat = test.find('ago')+3

我该使用哪个函数以整数为分隔符进行字符串分割?split()函数不支持整型参数。


将字符串视为数组-使用str [: splitat]表示法 - yoav_aaa
字符串是字符列表,支持切片。 - Francesco Montesano
Python字符串基本上是不可变的列表,你只需要使用切片就可以了,例如test[splitat: splitat+3]。 - Tbaki
如果您可以影响原始数据的外观,请尽管去做。您所使用的格式并不支持您。如果您无法这样做,请选择下面发布的解决方案之一。 - Ma0
3个回答

33
你可以使用字符串(和列表)的切片来完成这个操作:
string = "hello world!"
splitat = 4
left, right = string[:splitat], string[splitat:]

将导致:

>>> left
hell
>>> right
o world!

7
也许最简单的解决方案是使用字符串切片:
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:])

0

给你一个内置函数。

import re    
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'

m = re.search('ago', test)
response = test[m.end():]

这是正则表达式产生的对象:<re.Match object; span=(41, 44), match='ago'>

您可以使用 m.start() 获取第一个字符的位置,使用 m.end() 获取最后一个字符。

https://docs.python.org/3/library/re.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接