Skip to main content
 首页 » 编程设计

python之从字符串中获取两个数字

2025年05月04日22artech

我需要在字符串中提取两个数字。它们看起来像这样:

(0,0) 
(122,158) 
(1,22) 
(883,8) 
etc... 

所以我想获取逗号前的第一个数字和逗号后的数字,并将它们存储在变量中。我可以这样得到第一个数字:

myString.split(',')[0][1:]) 

但是,我不知道如何获得下一个数字。


Thanks for the help everyone!

请您参考如下方法:

它应该与类似的东西一起工作

myVar.split(',')[0][1:] # = 122 for the string in the second line 
myVar.split(',')[1][:-1] # = 158 for the string in the second line 

这应该是最简单的方法