本文整理汇总了Python中car.Car.get_nb_doors方法的典型用法代码示例。如果您正苦于以下问题:Python Car.get_nb_doors方法的具体用法?Python Car.get_nb_doors怎么用?Python Car.get_nb_doors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类car.Car
的用法示例。
在下文中一共展示了Car.get_nb_doors方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: open
# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import get_nb_doors [as 别名]
''' Open the json file and load it into a variable to be used later '''
with open('5-main.json') as json_data:
data = json.load(json_data)
all_brands = []
total_doors = 0
''' Create a DOM document '''
document = Document()
all_cars = document.createElement("all_cars")
document.appendChild(all_cars)
''' Iterate through data to format each dict '''
for i in data:
car = Car(i)
brand = car.get_brand()
nb_doors = car.get_nb_doors()
xml = car.to_xml_node(document)
if brand not in all_brands:
all_brands.append(brand)
total_doors += nb_doors
all_cars.appendChild(xml)
print len(all_brands)
print total_doors
last_car = Car(data[3])
print last_car
print document.toxml(encoding='utf-8')
示例2: open
# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import get_nb_doors [as 别名]
from xml.dom.minidom import Document
import json
from car import Car
'''initializations'''
filename = "5-main.json"
file_obj = open(filename)
cars_json = json.load(file_obj)
file_obj.close()
cars=[]
brands=[]
doors=0
doc = Document()
element_cars = doc.createElement('cars')
doc.appendChild(element_cars)
for car in cars_json:
c=Car(car)
cars.append(c)
if not c.get_brand() in brands:
brands.append(c.get_brand())
doors+=c.get_nb_doors()
c_xml = c.to_xml_node(doc)
element_cars.appendChild(c_xml)
print len(brands)
print doors
print cars[3]
print doc.toxml(encoding='utf-8')
示例3: open
# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import get_nb_doors [as 别名]
import json
from collections import Counter
'''Getting the information from a json file'''
with open("5-main.json") as json_file:
json_data = json.load(json_file)
doorsNum = 0
counter = 0
brands = []
fourth_car = ""
doc = Document()
cars = doc.createElement("Cars")
while counter < len(json_data):
c = Car(json_data[counter])
''' adding number of doors castied into ints '''
doorsNum += int(c.get_nb_doors())
'''Adding elements to the array brands '''
brands.append(c.get_brand())
if counter == 3:
fourth_car = c
counter +=1
'''Appending to root element cars '''
cars.appendChild(c.to_xml_node(doc))
doc.appendChild(cars)
''' set gets rid of the duplicates'''
print len(set(brands))
print doorsNum
print fourth_car
print doc.toxml(encoding='utf-8')
示例4: Car
# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import get_nb_doors [as 别名]
json_data = json_open.read()
data = json.loads(json_data)
json_open.close()
doc = minidom.Document()
cars = doc.createElement('cars')
doc.appendChild(cars)
brands = []
num_brands = 0
num_doors = 0
for f in data:
car = Car(f)
brands.append(car.get_brand())
num_doors = (num_doors + car.get_nb_doors())
car_xml = car.to_xml_node(doc)
cars.appendChild(car_xml)
i = 0
len_brands = len(brands)
while i < len_brands:
if brands[i] != brands[i -1]:
num_brands += 1
i += 1
print num_brands
print num_doors
print Car(data[3])
print doc.toxml(encoding='utf-8')
示例5: Car
# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import get_nb_doors [as 别名]
data = json.loads(json_file.read())
'''Initializes a XML document'''
xml_cars = doc.createElement('cars')
doc.appendChild(xml_cars)
'''Creates a list of car classes from JSON data'''
for car in data:
car = Car(car)
cars.append(car)
for car in cars:
'''Creates a list of unique brands'''
brand = car.get_brand()
if not brand in brands:
brands.append(brand)
'''Calculates a total of doors on all cars'''
doors += car.get_nb_doors()
'''Adds the car to the XML document'''
c_xml = car.to_xml_node(doc)
xml_cars.appendChild(c_xml)
'''Prints the number of brands, door count, the 4th car, and the XML doc'''
print len(brands)
print doors
print cars[3]
print doc.toxml(encoding='utf-8')
示例6: Document
# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import get_nb_doors [as 别名]
file_open.close()
'''initialize lists and variables'''
cars = []
brands = []
totalDoors = 0
doc = Document()
'''special XML stuff to create an element'''
element_cars = doc.createElement('cars')
'''special XML stuff to create a child element'''
doc.appendChild(element_cars)
'''Iterating through the json file to place
items in the list and update the number of
total doors'''
for item in diffCars:
obj = Car(item)
cars.append(obj)
if not obj.get_brand() in brands:
brands.append(obj.get_brand())
totalDoors = totalDoors + obj.get_nb_doors()
objXml = obj.to_xml_node(doc)
element_cars.appendChild(objXml)
print len(brands)
print totalDoors
print cars[3]
print doc.toxml(encoding = 'utf-8')
示例7: open
# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import get_nb_doors [as 别名]
'''copy originial JSON file content to new JSON file'''
json_file = open("5-main.json")
file_copy = json.load(json_file)
json_file.close()
'''arrays for cars and brands'''
cars = []
brands = []
'''initialize number of car doors'''
car_doors = 0
'''prepare DOM document and create root element'''
doc = minidom.Document()
all_cars = doc.createElement('cars')
doc.appendChild(all_cars)
for each in file_copy:
objects = Car(each)
cars.append(objects)
if objects.get_brand() not in brands:
brands.append(objects.get_brand())
car_doors += objects.get_nb_doors()
all_cars.appendChild(objects.to_xml_node(doc))
print len(brands)
print car_doors
print cars[3]
print doc.toxml(encoding = 'utf-8')