money_1=i.strip().split(",")[-3]
时间: 2024-06-04 11:11:45 浏览: 13
As an AI language model, I cannot provide a complete answer without additional context. However, based on the given code snippet, it appears that the line of code is extracting the third to last element of a comma-separated string (presumably from a file or input), and assigning it to the variable "money_1".
相关问题
import json from data_define import Record # 先定义一个抽象类用来做顶层设计,确定有那些功能需要实现 class FileReader: def read_data(self) -> list[Record]: """读取文件的数据,读到的每一条数据都转换为Record对象。将它们都封装到list内返回即可""" pass class TextFileReader(FileReader): def __init__(self,path): self.path = path # 定义成员变量记录文件的路径 # 复写(实现抽象方法)父类的方法 def read_data(self) -> list[Record]: f = open(self.path,"r",encoding="UFT-8") record_list: list[Record] = [] for line in f.readlines(): line = line.strip() # 消除读取到的每一行数据中的\n data_list = line.split(",") record = Record(data_list[0],data_list[1],int(data_list[2]),data_list[3]) record_list.append(record) f.close() return record_list class JsonFileReader(FileReader): def __init__(self,path): self.path = path def read_data(self) -> list[Record]: f = open(self.path,"r",encoding="UFT-8") record_list: list[Record] = [] for line in f.readlines(): data_dict = json.load((line)) record = Record(data_dict["data"],data_dict["order_id"],int(data_dict["money"]),data_dict("province")) record_list.append(record) f.close() return record_list if __name__ == '__main__': text_file_reader = TextFileReader("D:/2011年1月销售数据。txt") json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt") list1 = text_file_reader.read_data() list2 = json_file_reader.resa_data() for l in list1: print(l)
这段代码展示了一个简单的多态的例子。在这个例子中,`FileReader`是一个抽象类,定义了一个抽象方法`read_data()`,用于读取文件数据并返回`Record`对象的列表。`TextFileReader`和`JsonFileReader`是`FileReader`的子类,它们分别实现了`read_data()`方法来读取文本文件和JSON文件的数据。
通过多态性,我们可以使用父类类型的引用指向不同的子类对象,并调用相同的方法来实现不同子类对象的不同行为。在主程序中,创建了一个`TextFileReader`对象和一个`JsonFileReader`对象,并调用它们的`read_data()`方法来读取数据并返回`Record`对象的列表。然后,分别遍历这两个列表并打印每个`Record`对象的内容。
多态性的好处是可以在不修改现有代码的情况下,轻松地扩展和添加新的子类实现。它提高了代码的可维护性和灵活性。
def parse(self, response): res = Selector(response) items = RenrenchesipderItem() items['id'] = res.xpath('//div[@class="detail-wrapper"]/@data-encrypt-id').extract()[0] # 标题 items['title'] = res.xpath('//div[@class="title"]/h1/text()').extract()[0] # 客户出价 items['price'] = res.xpath('//div[@class="middle-content"]/div/p[2]/text()').extract()[0] # 市场价 items['new_car_price'] = res.xpath('//div[@class="middle-content"]/div/div[1]/span/text()').extract()[0] # 首付款 down_payment = res.xpath('//div[@class="list"]/p[@class="money detail-title-right-tagP"]/text()') # 月供 monthly_payment = res.xpath('//[@id="basic"]/div[2]/div[2]/div[1]/div[3]/div[2]/p[5]/text()') # 判断是否可以分期购买 if down_payment and monthly_payment: items['staging_info'] = [down_payment.extract()[0], monthly_payment.extract()[0]] # 服务费 items['service_fee'] = res.xpath('///div[1]/p[2]/strong/text()').extract()[0] # 服务项 items['service'] = res.xpath('//[@id="js-box-service"]/table/tr/td/table/tr/td/text()').extract() # 车辆上牌时间 里程 外迁信息 items['info'] = res.xpath('///div[2]/div[2]/div[1]/div[4]/ul/li/div/p/strong/text()').extract() # 车辆排量 items['displacement'] = \ res.xpath('//[@id="basic"]/div[2]/div[2]/div[1]/div[4]/ul/li[4]/div/strong/text()').extract()[0] # 车辆上牌城市 items['registration_city'] = res.xpath('///@licensed-city').extract()[0] # 车源号 items['options'] = \ res.xpath('//*[@id="basic"]/div[2]/div[2]/div[1]/div[5]/p/text()').extract()[0].strip().split(":")[1] # 判断是都有图片 if res.xpath('//div[@class="info-recommend"]/div/img/@src'): # 车辆图片 items['car_img'] = res.xpath('//div[@class="info-recommend"]/div/img/@src').extract()[0] # 车辆所在城市 items['city'] = res.xpath('//div[@rrc-event-scope="city"]/a[@class="choose-city"]/text()').extract()[0].strip() # 车辆颜色 items['color'] = res.xpath('//div[@class="card-table"]/table/tr/td[2]/text()').extract()[0] 解释一下每一句话
这是一个 Python 的 scrapy 爬虫程序中的一个解析函数,主要作用是从网页中提取出需要的数据并存储到 items 中。具体解释如下:
```python
def parse(self, response):
res = Selector(response) # 使用 scrapy 的 Selector 对象解析 response
items = RenrenchesipderItem() # 定义一个 items 对象用于存储数据
# 从网页中提取出 id、标题、客户出价、市场价、首付款、月供、服务费、服务项、车辆上牌时间、里程、外迁信息、车辆排量、车辆上牌城市、车源号、车辆颜色等数据
items['id'] = res.xpath('//div[@class="detail-wrapper"]/@data-encrypt-id').extract()[0]
items['title'] = res.xpath('//div[@class="title"]/h1/text()').extract()[0]
items['price'] = res.xpath('//div[@class="middle-content"]/div/p[2]/text()').extract()[0]
items['new_car_price'] = res.xpath('//div[@class="middle-content"]/div/div[1]/span/text()').extract()[0]
down_payment = res.xpath('//div[@class="list"]/p[@class="money detail-title-right-tagP"]/text()')
monthly_payment = res.xpath('//[@id="basic"]/div[2]/div[2]/div[1]/div[3]/div[2]/p[5]/text()')
if down_payment and monthly_payment:
items['staging_info'] = [down_payment.extract()[0], monthly_payment.extract()[0]]
items['service_fee'] = res.xpath('///div[1]/p[2]/strong/text()').extract()[0]
items['service'] = res.xpath('//[@id="js-box-service"]/table/tr/td/table/tr/td/text()').extract()
items['info'] = res.xpath('///div[2]/div[2]/div[1]/div[4]/ul/li/div/p/strong/text()').extract()
items['displacement'] = res.xpath('//[@id="basic"]/div[2]/div[2]/div[1]/div[4]/ul/li[4]/div/strong/text()').extract()[0]
items['registration_city'] = res.xpath('///@licensed-city').extract()[0]
items['options'] = res.xpath('//*[@id="basic"]/div[2]/div[2]/div[1]/div[5]/p/text()').extract()[0].strip().split(":")[1]
if res.xpath('//div[@class="info-recommend"]/div/img/@src'):
items['car_img'] = res.xpath('//div[@class="info-recommend"]/div/img/@src').extract()[0]
items['city'] = res.xpath('//div[@rrc-event-scope="city"]/a[@class="choose-city"]/text()').extract()[0].strip()
items['color'] = res.xpath('//div[@class="card-table"]/table/tr/td[2]/text()').extract()[0]
return items # 返回 items 对象
```
其中,Selector 对象是 scrapy 中提供的一种选择器,可以方便地从 HTML 或 XML 中提取数据。extract() 方法用于获取提取的数据,[0] 则是因为返回的是一个列表,需要取出其中的第一个元素。此外,还有一些判断语句用于处理数据缺失或判断是否存在该数据,如 if down_payment and monthly_payment: 判断是否可以分期购买。最后,将所有数据存储在 items 对象中并返回。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)