简体   繁体   English

从 python 字符串中提取数字

[英]Extract digits from python string

I want to extract digits from a python string into key value pairs.我想从 python 字符串中提取数字到键值对中。

Original string is TOTAL Earnings 117,050.00 or WCPS Contribution -1,671.00 want the result to be {'TOTAL Earnings': 117050} or {'WCPS Contribution': -1671}原始字符串为TOTAL Earnings 117,050.00WCPS Contribution -1,671.00希望结果为{'TOTAL Earnings': 117050}{'WCPS Contribution': -1671}

You could split the string and convert it to dict:您可以拆分字符串并将其转换为 dict:

s='TOTAL Earnings 117,050.0'
l=s.rsplit(' ',1)
l[1]=int(float(l[1].replace(',','')))
d=dict([l])

Result:结果:

{'TOTAL Earnings': 117050}

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

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