Python数据抓取差异-百万与百万
我目前正在互联网上抓取一些表格,其中数字以不同的数字格式发布:Python数据抓取差异-百万与百万,python,pandas,csv,web-scraping,python-requests,Python,Pandas,Csv,Web Scraping,Python Requests,我目前正在互联网上抓取一些表格,其中数字以不同的数字格式发布: Animal - Left in Wild Tigers - 18 Deer - 18m Pigs - 180000 我已设法将m从数字中去掉,但我想知道是否/如何使用if语句进行一些操作,以确保我准确记录数字: if animal.strip("m") == animal.strip("m"): left_in_wild = left_in_wild * 1000000 很明显,这段代码不起作用,但这是一个粗略的想法,
Animal - Left in Wild
Tigers - 18
Deer - 18m
Pigs - 180000
我已设法将m从数字中去掉,但我想知道是否/如何使用if语句进行一些操作,以确保我准确记录数字:
if animal.strip("m") == animal.strip("m"):
left_in_wild = left_in_wild * 1000000
很明显,这段代码不起作用,但这是一个粗略的想法,我正在考虑如何绕过这一点。如果有人能提供他们认为有帮助的东西,请告诉我
谢谢大家! 类似于:
import re
def get_number(s):
try:
i=int(re.match('(\d+)', s).group(1))
if "m" in s:
i*=1000000
return i
except:
print "No Number"
获取数字(“18m”)
返回18000000
如果您有数千个或更多,甚至可以将其扩展为在s块中有一个
elif“k”。一个简单的if语句可以帮助您查找:
animal = "18m"
if 'm' in animal:
print animal.strip('m') + ",000,000"
if 'k' in animal:
print animal.strip('k') + ",000"
返回:
18,000,000
如果要当场编译表达式,请改用re.match('(\d+)',s.group(1)
。如果您想使用预编译版本,请将其置于函数之外,或者将其设置为默认参数。很好的调用。更新。谢谢