【发布时间】:2018-08-03 22:10:30
【问题描述】:
import csv
from datetime import date
def calcAge(born): # calculates age
today = date.today()
return today.year - born.year - ((today.month, today.day) <
(born.month, born.day)
byear = int(input("year of birth"))
fname = input("first name")
lname = input("last name")
dob = input("date of birth")
address = input("address")
age = calcAge(bornyear)
Data = [["FirstName", "SecondName", "DateOfBirth", "Address"],
[fname, lname, dob, age, byear, address]]
File = open("CustomerRecords2.csv", "a+")
with File:
writer = csv.writer(File)
writer.writerows(Data)
print ("done")
这段代码是写一个CSV文件,但是在变量行上,好像有我添加函数时才出现的语法错误。
【问题讨论】:
-
我不知道你的确切意思是什么,但我看到两个问题:(1)
((today.month, today.day) < (born.month, born.day)后面缺少右括号,(2)with File:后面的行需要缩进。对于 (1),使用高亮匹配括号的文本编辑器会有所帮助。 -
(3)
bornyear未定义。 -
感谢您的反馈,我是菜鸟,哈哈
标签: python-3.x csv syntax