Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python获取字符串的一部分_Python - Fatal编程技术网

使用python获取字符串的一部分

使用python获取字符串的一部分,python,Python,我正在用python处理字符串。我有一根像这样的线: 'http://pbs.twimg.com/media/xxiajdiojadf.jpg||2013-11-17,16:19:52||more text in this string' 获取字符串的第一部分和第二部分很容易,但是对于第二部分我需要做什么?我的意思是,我想获取第二部分之后的文本。对于第一个: url=s.split("||")[0] and date=s.split("||")[1] 我尝试过使用url=s.split(“

我正在用python处理字符串。我有一根像这样的线:

'http://pbs.twimg.com/media/xxiajdiojadf.jpg||2013-11-17,16:19:52||more text in this string'
获取字符串的第一部分和第二部分很容易,但是对于第二部分我需要做什么?我的意思是,我想获取第二部分之后的文本。对于第一个:

url=s.split("||")[0] and  date=s.split("||")[1]
我尝试过使用
url=s.split(“| |”)[2]
,但我什么都没有
提前感谢

您可以使用第二个索引:

s.split("||")[2]
输出:

'more text in this string'
split将返回列表

>>> s.split("||")
['http://pbs.twimg.com/media/xxiajdiojadf.jpg', '2013-11-17,16:19:52', 'more text in this string']
>>> url,date,extra = s.split("||")
>>> print(url)
'http://pbs.twimg.com/media/xxiajdiojadf.jpg'
>>> print(date)
'2013-11-17,16:19:52'
>>> print(extra)
'more text in this string'

我想你刚才打错了。你的逻辑是正确的。在第三行中,应该使用另一个变量,而不是“url”

更简洁的方法是:

url, date, descr = s.split("||")

奇怪,它在我的Python 3.6.9中工作>>s='此字符串中的文本'>>>s'此字符串中的文本'>>>s|u list=s.split('| |')>>>s_list['','2013-11-17,16:19:52','此字符串中的更多文本']>>s_list[0]'>>s|列表[1]'2013-11-17,16:19:52'>>s|列表[2]“此字符串中的更多文本”您尝试的应该是work@zoit您可以输入错误和python版本吗?