【发布时间】:2018-03-27 18:01:51
【问题描述】:
如何按单词的位置拆分字符串?
我的数据如下所示:
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() 函数不适用于 int。
【问题讨论】:
-
将字符串视为数组 - 使用 str[:splitat] 表示法
-
字符串是字符列表,支持切片
-
Python 字符串基本上是不可变列表,我认为你可以使用 slice,例如 test[splitat:splitat+3]。
-
如果您可以影响原始数据的外观,那就去做吧。您必须使用的格式根本不利于您。如果您不能,请选择下面发布的解决方案之一。