如何将 python 中从字符串中提取的数据的数据类型更改为金钱或小数

How can I change the data type of data that is being extracted from string to money or decimal in python

我正在尝试从网站中提取数据,我可以成功提取数据,但我遇到的挑战是价格项是以字符串形式存储的,我如何修改我的代码以便价格在我的 csv 文件中存储为小数还是货币?

def parse(self,response):
    count = 0
    for tv in response.xpath("//div[@class='products wrapper grid products-grid']/ol/li[@class='item product product-item']"):
        count +=1
        url = tv.xpath(".//div[@class='product-item-info']/div[@class='product details product-item-details']/strong/a/@href").get()
        name = tv.xpath(".//div[@class='product-item-info']/div[@class='product details product-item-details']/strong/a/text()").get()
        old_price = tv.xpath(".//div/span[@class='old-price']/span/span/span[@class='price']/text()").get()
        img = tv.xpath(".//div[@class='product-item-info']/a/span/span/img/@src").get()
        special_price = tv.xpath(".//div/span[@class='special-price']/span/span/span[@class='price']/text()").get()
        price = tv.xpath(".//div/div/span/span[@class='price-wrapper ']/span[@class='price']/text()").get()
   
               
        yield {
        'ID': count,
        'Name': name.strip() if name else name,
        'Description' : None,
        'Product_URL': url.strip() if url else url,
        'Old_Price': old_price.strip() if old_price else old_price, 
        'Special_Price' : special_price.strip()if special_price else special_price, 
        'Final_Price' : price.strip() if price else price,
        'Image_URL': img.strip() if img else img,
        'Category_ID' : 1
               

货币换算,你可以试试这个:

from re import sub
from decimal import Decimal

str_value = 'R3,699.99'
numeric_value = Decimal(sub(r'[^\d.]', '', money))