Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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字符串中获取float和int_Python_Regex_Pandas - Fatal编程技术网

如何从python字符串中获取float和int

如何从python字符串中获取float和int,python,regex,pandas,Python,Regex,Pandas,我试图使用这段代码从字符串中获取一些int和float。我面临的问题是m=re.findall(“\d+\.\d+\d+”,y)没有像我预期的那样工作 输入数据帧为: for key in x: if(key=='data'): dd = dd.from_dict(x[key]) dd = dd[:5] for row in dd.iterrows(): y = str(row) m = re.findall("\d+\.\d+|\d+

我试图使用这段代码从字符串中获取一些int和float。我面临的问题是
m=re.findall(“\d+\.\d+\d+”,y)
没有像我预期的那样工作

输入数据帧为:

for key in x: 
    if(key=='data'):
        dd = dd.from_dict(x[key])
        dd = dd[:5]

for row in dd.iterrows():
    y = str(row)
    m = re.findall("\d+\.\d+|\d+",y)
    print(m)
输出为:

0  [2019-10-14T09:15:00+0530, 232.55, 235.2, 231.7, 233, 80683, 0]    
1  [2019-10-14T09:20:00+0530, 233, 233, 231.4, 231.8, 53296, 0]       
2  [2019-10-14T09:25:00+0530, 231.8, 232.8, 231.1, 231.2, 41238, 0]   
3  [2019-10-14T09:30:00+0530, 231.25, 231.4, 230.6, 231.4, 31558, 0]  
4  [2019-10-14T09:35:00+0530, 231.4, 231.75, 230.95, 231.05, 29480, 0]
预期产出为:

['0', '2019', '10', '14', '09', '15', '00', '0530', '232.55', '235.2', 
'231.7', '233', '80683', '0', '0']
['1', '2019', '10', '14', '09', '20', '00', '0530', '233', '233', '231.4', 
'231.8', '53296', '0', '1']
['2', '2019', '-10', '-14', '09', '25', '00', '0530', '231.8', '232.8', '231.1', '231.2', '41238', '0', '2']

如果日期总是列表中的第一个,则可以简单地使用str内置的.split()方法以及列表切片

就你而言,我相信

[ '232.55', '235.2', '231.7', '233', '80683','0']
[ '233', '233', '231.4', '231.8', '53296','0']
[ '231.8', '232.8', '231.1', '231.2', '41238','0']


如果您希望处理的元素始终是第一个元素,则应起作用。

输出为:['0','2019','-10','-14','09','15','00','0530','232.55','235.2','231.7','233','80683','0','0']['1','2019','-10','14','09','20','00','0530','233','233','231.4','231.8','53296','0','1'][2',2019',-10',-14',09',25',00',0530',231.8',232.8',231.1',231.2',41238',0',2']现在问题已更新,您将获得更好的问题答案。
m = re.findall("\d+\.\d+|\d+",y)
m = y.split(',')[1:] #splits 'y' str into list and takes elements from index 1 to end