Python 如何在单独的文件上写入数组?

Python 如何在单独的文件上写入数组?,python,arrays,file,Python,Arrays,File,我试图将日期保存到一个单独文件(“file.txt”)中的数组(log[])。 我该怎么做 我已经打开了文件本身,但我不知道如何打开该文件中的log[]并向其添加数据 x = input("Please Enter Name: ") y = input("Please Enter Phone Number: ") z = input("Please Enter Room Number: ") with open("log.txt", "a") as f: with open(

我试图将日期保存到一个单独文件(“file.txt”)中的数组(log[])。 我该怎么做

我已经打开了文件本身,但我不知道如何打开该文件中的log[]并向其添加数据

x = input("Please Enter Name: ")
  y = input("Please Enter Phone Number: ")
  z = input("Please Enter Room Number: ")
  with open("log.txt", "a") as f:
    with open("log[]", "a") as g:
      g.write("Name: " + x)
      g.write("Phone Number: " + y)
      g.write("Room Number: " + z)

when doing this, it opened up a new file called log[] and saved it into there, not the actual array in log.txt. Then log.txt displayed that the login crashed

您可以使用此python脚本解决您的问题

x = input("Please Enter Name: ")
y = input("Please Enter Phone Number: ")
z = input("Please Enter Room Number: ")

# {} are the place holder of x, y and z
# \n means new line at the end of each write done in the file.
log = '[{}, {}, {}]\n'.format(x, y, z)

# footxt in the same dir as the python script.
file_handler = open('footxt.txt', 'a')
file_handler.writelines(log)
file_handler.close()
当脚本运行2次时,脚本的输出在
foo.txt
文件中如下所示

[姓名,8888801]

[姓名2,998878,02]

将foo.txt文件打印到屏幕的代码

file_handler = open('footxt.txt')

for line in file_handler.readlines():
    print (line)

file_handle.close()

您能告诉我您希望在文件中写入什么吗?我正在尝试将三个值保存到文件的数组中,并能够在以后调用它们。这有助于编写或标记语言。您没有从
log.txt
中读取任何内容。您只是将从输入读取的值附加到
log[]
谢谢-我如何从文件中检索这些值并将它们打印到originia程序上?