Python - 将数组写入csv / txt,然后阅读

时间:2017-11-21 18:17:29

标签: python csv text

所以,如果我有以下代码:

database = []
Name = input("What's the members name?")
MT = input("What membership type?")
DOB = input("What is the member DOB?")
DJ = (now.day ,"/" , now.month, "/", now.year)
year1 = int(now.year)
month1 = int(now.month)
day1 = int(now.day)
ry = int(year1 + 1)
rm = month1
rd = day1
renewal = (day1, month1, year1 + 1)
details = ["Name:",  Name, "Membership Type:", MT, "Date of Birth:", DOB, "Date Joined:", DJ,"Renewal Date:", renewal, "Renewal Year:", ry]
database.append(details)
menu()  

我如何保存(数据库)文本文件/ csv文件以便稍后阅读 我尝试过泡菜,但问题是我需要能够将阵列的每个部分分开 例如,如果我输入:

print(database[1])

我将返回存储为“Name”的所有名称,但是如果我在将其带回来时使用pickle写文件

print (database[1])

现在显示添加的第二个用户的全部内容。我需要能够保存(数据库)并在读回时以相同的格式使用它。 使用JSON或Pickle的预期输出(假设第一个用户输入的名称为“Jeff”,第二个输入的名称为“John”)

print (database[1])

Jeff
John

实际输出:

["Name:",  John, "Membership Type:", MT, "Date of Birth:", DOB, "Date Joined:", DJ,"Renewal Date:", renewal, "Renewal Year:", ry]
  • 很抱歉我不熟悉python。

1 个答案:

答案 0 :(得分:0)

您似乎正在创建一系列记录。尝试以这种方式访问​​数据:

print(database[0][1])    #[0] for the first record, [1] for second item
print(database[1][1])    #second user, second item