Skip to main content
 首页 » 编程设计

python之如何在一个文件的一行中输出 3 个项目

2025年05月04日15grandyang

我想制作一个程序,允许用户连续输入一组三项,每个输入提示一个,然后将这三个值输出到文本文件中的一行。

这是一些基本的例子

text_file = open("filename", "w") 
 
While True: 
      Code = input("Enter code") 
      Description = input("Enter description") 
      Price = input("Enter price") 

谁能帮帮我?

编辑: 这就是我到目前为止的情况。

text_file = open("file.txt", "w") 
 
while True: 
      user_input = input("Enter a code, description and price") 
      split = user_input.split(" ") 
      split = str(split) 
      text_file.write(split) 

唯一的问题是它不让我输出列表。

请您参考如下方法:

sep = ','   # separator character 
textfile.write(sep.join([Code, Description, Price]) + '\n')