Python:在包含2个变量字符串和一个set字符串之后不支持Str的解码

2024-10-02 16:31:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在写一个课程作为学校课程的一部分,并决定使它比我需要的更广泛一些。所以在三个月的大部分时间里,我一直在慢慢地添加一些东西,试图让系统输出一个文件来保存,让人可读。你知道吗

我可以把这些都保存在一个文件下,就像我以前写的程序一样,但我只有努力才能做得更好。所以我让程序把用户的姓氏和到达日期作为文件名。我让程序将这些变量(两个字符串)与“.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.

Tags: ofthenameininputiscurrentroom
1条回答
网友
1楼 · 发布于 2024-10-02 16:31:08
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)

相关问题 更多 >