简体   繁体   English

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

[英]Python: split a string by the position of a character

How can I split a string by the position of a word?如何按单词的位置拆分字符串?

My data looks like this:我的数据如下所示:

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'

I need this output:我需要这个输出:

responder = 'annamarypeterson, Guest relations Manager'
date = 'Responded 1 week ago'
response = 'Dear ....' #without 'This response is the subjective opinion of the management representative'

I know that the find.() function gives the position of a word, and I want to use this position to tell Python where to split it.我知道find.()函数给出了一个单词的位置,我想用这个位置来告诉 Python 在哪里拆分它。 For example:例如:

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

What function can I use to split with an integer?我可以使用什么函数来分割整数? The split() function does not work with an int. split()函数不适用于 int。

You can do this with strings (and lists) using slicing: 您可以使用切片对字符串(和列表)执行此操作:

str = "hello world!"
splitat = 4
l, r = str[:splitat], str[splitat:]

will result in: 将导致:

>>> l
hell
>>> r
o world!

Maybe the simplest solution is to use string slicing: 也许最简单的解决方案是使用字符串切片:

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:])

Got a built in function for you.为您提供了一个内置功能。

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():]

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

You can use m.start() to get the position of the first character and m.end() to get the last character.您可以使用 m.start() 获取第一个字符的位置,使用 m.end() 获取最后一个字符的位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM