我的程序应该将答案输出到具有特定格式的csv文件中

时间:2017-01-08 19:14:40

标签: python-3.x csv

这是代码

import datetime

mylist = []
today = datetime.date.today()
mylist.append(today)
print(mylist[0])

file = open('Date.txt', 'a')
file.write(mylist)
file.close()
global timestable
global text
def timestable():
    global name
    name = input("What is your name? ")
    global number
    number = int(input("Insert a number you want the 12 times table for: "))
    global multiply
    for multiply in range(1,13):
        print(number,"x",multiply,"=",number*multiply)
        text()

def text():
    file=open("list.csv","a")
    file.write("The date is," + today + "\nTimestables are:\n" + number + "x" + multiply + "=" + number*multiply + "\nYour name is, " + name)
    file.close()
    text()
timestable()

问题是文件中没有输出任何内容,并且它也应该以某种格式输出。

错误是

Traceback (most recent call last):
  File "D:\Python\Code\Lesson Task Code.py", line 9, in <module>
    file.write(mylist)
TypeError: must be str, not list

1 个答案:

答案 0 :(得分:0)

而不是datetime.date.today()您可能想要试用time.strftime("%Y-%m-%d")

from time import strftime

today = strftime("%Y-%m-%d")
print(today)

with open('Date.txt', 'a') as file:
    file.write(today+"\n")