Python:在连接2个变量字符串和一个集合字符串后,不支持对Str进行解码



我一直在写一个程序,作为学校课程的一部分,并决定让它比我需要的更广泛一点。因此,在3个月的大部分时间里,我一直在慢慢添加一些东西,试图让系统输出一个可供人类阅读的文件来保存。

我可以像以前写的程序一样,把这些都保存在一个文件中,但如果我尝试,我只能变得更好。所以我让这个程序把用户的姓氏和到达日期作为文件名。我让程序将这些变量(两个字符串)与末尾的".txt"连接起来,并将其保存为一个变量,然后用它来命名我想写入的文件。问题是它会抛出"不支持解码字符串"的错误。只是为了参考,这个程序是为了预订一个类似酒店的东西,并说出价格、到达日期、姓名等,然后打印出来,但正如我所说,我太雄心勃勃了。此外,我为错误的代码道歉,今年才开始学习。感谢您的帮助。必须通过Python 3.3.0:运行它

#Comment Format
#
#Code
#
#Comment about above Code
from random import*
c_single = 47
c_double = 90
c_family = 250
discount = 10
VAT = 20
max_stay = 14
min_stay = 1
room_list = []
min_rooms = 1
max_rooms = 4
cost = 0
#Sets all needed variables that would need to be changed if the physical business changes (e.g. Increase in number of rooms, Longer Max stay allowed, Change to Pricing)
print("Cost of room:")
print("Single - £", c_single)
print("Double - £", c_double)
print("Family - £", c_family)
#Prints prices based on above variables
name = input("What is the family name --> ")
arrival = input("Please enter date of arrival in the form dd/mm/yy --> ")
while len(arrival) != 8:
arrival = input("Please re-enter, in the case of the month or day not being 2 digits long insert a 0 before to insure the form dd/mm/yy is followed --> ")
#Gets the guests name and date of arrival for latter update into the business' preferred method of storage for bookings
nights = int(input("How many nights do you intend to stay in weeks --> "))
while nights > max_stay or nights < min_stay:
nights = int(input("That is too short or long, please reneter stay in weeks -->"))
if nights >=  3:
discount_aplic = 1
#Gets the guests ideal number of weeks stay, ensure that this would be possible then adds the discount if applicable
rooms = int(input("Please enter number of rooms required --> "))
while rooms < min_rooms or rooms > max_rooms:
rooms = int(input("That number of rooms is unachievable in one purchase, please re-enter the number of rooms you require --> "))
#Gets number of rooms desired and checks that it does not exceed the maximum allowed by the business or the minimum (currently 1, staying no nights doesn't work)
for room in range (rooms):
current_room = input("Please enter the room desired--> ")
current_room = current_room.upper()
if current_room == "SINGLE":
cost += c_single
elif current_room == "DOUBLE":
cost += c_double
elif current_room == "FAMILY":
cost += c_family
#Checks which room is desired
else:
while current_room != "SINGLE" and current_room != "DOUBLE" and current_room != "FAMILY":
current_room = input("Invalid room type, valid entries are : single, double or family --> ")
current_room = current_room.upper()
#Ensures that the desired room is valid, if first inserted not correctly, repeats until valid entry is entered
room_list.append (current_room)
#Adds the wanted room type to the array room_list
cost = cost * nights
#calculates cost
booking = randrange(1000,9999)
print("Name: ", name)
print("You have booked from ", arrival)
print("You have booked stay for ", nights, "weeks")
print("You have booked", rooms, " room/s of these categories;")
print(str(room_list))
print("This will cost £", cost)
print("Booking referral: ", booking)
#Prints booking information
dateStr = str(arrival)
storageFileName = str(dateStr, name,".txt")
storageFile = open(storageFileName, "w")
storageFile.write("Name: ", name)
storageFile.write("They have booked from ", arrival)
storageFile.write("They have booked stay for ", nights, "weeks")
storageFile.write("They have booked", rooms, " room/s of these categories;")
storageFile.write(str(room_list))
storageFile.write("This has cost them -- >£", cost)
storageFile.write("Booking referral: ", booking)
#saves the storage data to a server under the name and data.
storageFileName = str(dateStr, name,".txt")

使用多个参数调用str不会将每个参数转换为字符串并将其组合。这里实际要做的是使用参数str(bytes_or_buffer, encoding, errors)调用str。根据文件:

>>> help(str)
Help on class str in module builtins:
class str(object)
|  str(object='') -> str
|  str(bytes_or_buffer[, encoding[, errors]]) -> str
|
|  Create a new string object from the given object. If encoding or
|  errors is specified, then the object must expose a data buffer
|  that will be decoded using the given encoding and error handler.
|  Otherwise, returns the result of object.__str__() (if defined)
|  or repr(object).
|  encoding defaults to sys.getdefaultencoding().
|  errors defaults to 'strict'.

由于要指定encodingerrors,所以第一个参数不能是字符串,因为字符串不是数据缓冲区。这解释了错误decoding str is not supported

如果您试图将字符串连接在一起,则应该使用+运算符或format方法:

storageFileName = dateStr + name + ".txt"

storageFileName = "{}{}.txt".format(dateStr, name)

最新更新