Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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_Pandas_Csv_Web Scraping_Python Requests - Fatal编程技术网

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)
。如果您想使用预编译版本,请将其置于函数之外,或者将其设置为默认参数。很好的调用。更新。谢谢