简体   繁体   English

如何获得两个子字符串之间的字符串?

[英]How to get a string between two substrings?

I have the following text from a text file. 我有一个文本文件中的以下文本。

#AAA#WantedData#bbb#ccc#ddd#eee#SoOn#

I want to get only the WantedData from the above string. 我只想从上面的字符串中获取WantedData Always I want to get the data between second and third # sign. 我一直想得到第二个和第三个#号之间的数据。

What is the efficient way to achieve this in python ? python实现此目的的有效方法是什么?

Python String split() Method Python字符串split()方法

The split() method splits a string into a list. split()方法将字符串拆分为列表。

You can specify the separator, default separator is any whitespace. 您可以指定分隔符,默认分隔符是任何空格。

data.split("#") # ['', 'AAA', 'WantedData', 'bbb', 'ccc', 'ddd', 'eee', 'SoOn', '']

you can do it just like this(if you always want data between second and third #sign.): 您可以像这样进行操作(如果您始终需要第二个和第三个#sign之间的数据。):

data = "#AAA#WantedData#bbb#ccc#ddd#eee#SoOn#"
print (data.split("#")[2])

output: 输出:

WantedData

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

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