在Python中将用户输入导出到txt文件时遇到问题
长话短说,我一直在开发一个Python程序,我一直在尝试将用户输入到程序中的内容转换成txt文件——我在网上得到了一些帮助,但在这里有点困惑。。需要它包括已输入车辆的索引、品牌、型号、颜色、年份和里程数(基本上是用户在主菜单中点击“4”时在输出中可见的内容)。其想法是,当用户点击“6”时,列表/数据将导出到名为“车辆清单””的txt文件中,然后退出应用程序。问题肯定是在elif ch==6:的代码末尾。。。对此,如有任何帮助或解决方案,我们将不胜感激!!以下是我到目前为止的情况:在Python中将用户输入导出到txt文件时遇到问题,python,Python,长话短说,我一直在开发一个Python程序,我一直在尝试将用户输入到程序中的内容转换成txt文件——我在网上得到了一些帮助,但在这里有点困惑。。需要它包括已输入车辆的索引、品牌、型号、颜色、年份和里程数(基本上是用户在主菜单中点击“4”时在输出中可见的内容)。其想法是,当用户点击“6”时,列表/数据将导出到名为“车辆清单””的txt文件中,然后退出应用程序。问题肯定是在elif ch==6:的代码末尾。。。对此,如有任何帮助或解决方案,我们将不胜感激!!以下是我到目前为止的情况: # autom
# automobile class
class automobile:
# constructor
def __init__(self, make="", model="", color="", year=2018, mileage=0):
self.make = make
self.model = model
self.color = color
self.year = year
self.mileage = mileage
# setter methods
def set_make(self, make):
self.make = make
def set_model(self, model):
self.model = model
def set_color(self, color):
self.color = color
def set_year(self, year):
self.year = year
def set_mileage(self, mileage):
self.mileage = mileage
# getter methods
def get_make(self):
return self.make
def get_model(self):
return self.model
def get_color(self):
return self.color
def get_year(self):
return self.year
def get_mileage(self):
return self.mileage
# end of automobile class
# method to add a new vehicle to the inventory
def add_vehicle(v_list):
make = input("Enter make: ")
model = input("Enter model: ")
color = input("Enter color: ")
year = int(input("Enter year: "))
mileage = int(input("Enter mileage: "))
v = automobile(make, model, color, year, mileage)
v_list.append(v)
print("*** VEHICLE ADDED SUCCESSFULLY...")
# method to remove a vehicle from the inventory
def remove_vehicle(v_list):
index = int(input("Enter the index # of vehicle you would like to remove: "))
if index >= 0 and index < len(v_list):
make = v_list[index].get_make()
model = v_list[index].get_model()
v_list.pop(index)
print(make, model, "HAS BEEN REMOVED FROM INVENTORY !")
else:
print("*** INVALID INDEX #... PLEASE TRY AGAIN")
# method to update a vehicle info in the inventory
def update_vehicle(v_list):
index = int(input("Enter the index # of vehicle you would like to update: "))
if index >= 0 and index < len(v_list):
make = input("Enter new make: ")
model = input("Enter new model: ")
color = input("Enter new color: ")
year = int(input("Enter new year: "))
mileage = int(input("Enter new mileage: "))
v_list[index].set_make(make)
v_list[index].set_model(model)
v_list[index].set_color(color)
v_list[index].set_year(year)
v_list[index].set_mileage(mileage)
print("*** UPDATED SUCCESSFULLY !")
else:
print("*** INVALID INDEX #... PLEASE TRY AGAIN")
# method to print all vehicle details in proper formatted order
def display_vehicles(v_list):
print('{:10} {:10} {:10} {:10} {:10} {:10}'.format('INDEX #', 'MAKE', 'MODEL', 'COLOR', 'YEAR', 'MILEAGE'))
for i in range(len(v_list)):
v = v_list[i]
print('{:10} {:10} {:10} {:10} {:10} {:10}'.format(str(i), v.get_make(), v.get_model(), v.get_color(), str(v.get_year()), str(v.get_mileage())))
v_list = [] # initial list
# looping infinitely
while True:
# showing menu
print("1. Add a vehicle")
print("2. Remove a vehicle")
print("3. Update a vehicle")
print("4. Display all vehicle inventory")
print("5. Exit")
print("6. Export to 'Vehicle Inventory' txt file and Exit")
# getting choice
ch = int(input("*** PLEASE CHOOSE AN OPTION: "))
# performing actions based on choice
if ch == 1:
add_vehicle(v_list)
elif ch == 2:
remove_vehicle(v_list)
elif ch == 3:
update_vehicle(v_list)
elif ch == 4:
display_vehicles(v_list)
elif ch == 5:
break;
elif ch == 6:
with open('Vehicle Inventory.txt', 'w') as filehandle:
for display_vehicles in v_list:
filehandle.write("%s\n" % display_vehicles)
break;
else:
print("*** INVALID OPTION... PLEASE TRY AGAIN")
#汽车类
等级汽车:
#建造师
def _u初始(自我,品牌=”,型号=”,颜色=”,年份=2018,里程=0):
自制
self.model=model
self.color=颜色
self.year=年
自我里程=里程
#setter方法
def set_make(自我,制造):
自制
def set_型号(自身,型号):
self.model=model
def设置颜色(自身,颜色):
self.color=颜色
def设置年(自身,年):
self.year=年
def set_里程(自身、里程):
自我里程=里程
#吸气剂法
def get_make(自我):
自制
def get_型号(自):
回归自回归模型
def get_颜色(自身):
返回自我颜色
def获取年度(自我):
回归自我年
def get_里程(自我):
返回自我里程
#汽车班结束
#将新车辆添加到库存的方法
def添加车辆(车辆列表):
make=输入(“输入make:”)
模型=输入(“输入模型:”)
颜色=输入(“输入颜色:”)
年份=整数(输入(“输入年份:”)
里程=整数(输入(“输入里程:”)
v=汽车(品牌、型号、颜色、年份、里程)
v_列表。附加(v)
打印(“***车辆添加成功…”)
#从库存中删除车辆的方法
def拆卸车辆(车辆列表):
index=int(输入(“输入要删除的车辆的索引:”)
如果索引>=0且索引=0且索引
执行代码时,txt文件包含以下行:
<__main__.automobile object at 0x0000017F5E7017C0>.
输出
make: car, model: a, color: red, year: 2020, mileage:20
make: car2, model: b, color: black, year: 10, mileage: 10
格式的简要说明
格式允许您使用基于占位符的样式将值插入字符串{}
对应于匿名占位符:这意味着该位置的值取决于.format(value1,value2,…)
中使用的顺序。
或者,您可以使用命名占位符,例如:
“我的车是{color},我是{year}买的”。格式(color=“red”,year=2010)
关于txt文件 就我个人而言,我不会使用txt文件,尤其是当您只对几行或每行的某个值感兴趣(例如:所有红色汽车)时,从这种类型的文件加载数据可能会非常烦人。我不知道txt文件是否对您是必需的,因此我不会提供关于以下替代方法的任何详细信息。在任何情况下,您都应该知道还有其他类型的文件被广泛用于存储信息
- Json。数据存储在字典中,即一组键值对。多亏了这个模块,读写文件变得很简单
- Csv。这与txt非常相似,但具有更结构化的形式,就像它是一个表一样。每行对应一条记录,每条记录由列组成。多亏了,csv很容易用于
make: car, model: a, color: red, year: 2020, mileage:20 make: car2, model: b, color: black, year: 10, mileage: 10