难以置信地围绕着Python导入。我已经读过了导入X和X导入*这里的区别 - http://effbot.org/zone/import-confusion.htm,但我很难理解导入“模块”和“模块”导入*之间的区别。特别是因为effbot文章建议坚持使用导入“模块”,这在这里不起作用。Python导入解释
此代码
import csv
import time
import datetime
startdate = time.strptime('31-Dec-50', "%d-%b-%y")
enddate = time.strptime('1-Jan-00', "%d-%b-%y")
with open('Classroom Utilization.csv', 'rb') as csvfile:
file = csv.DictReader(csvfile, delimiter=',')
for row in file:
checkstartdate = time.strptime(row['startdate'], "%d-%b-%y")
checkenddate = time.strptime(row['enddate'], "%d-%b-%y")
if checkstartdate < startdate:
startdate = checkstartdate
if checkenddate > enddate:
enddate = checkenddate
print time.strftime("%d-%b-%y", startdate)
print time.strftime("%d-%b-%y", enddate)
print "pre convert: " + str(startdate)
print "pre convert: " + str(enddate)
startdate = datetime.fromtimestamp(mktime(startdate))
enddate = datetime.fromtimestamp(mktime(enddate))
print "post convert: " + str(startdate)
print "post convert: " + str(enddate)
print '%s/%s/%s' % (startdate.month, startdate.day , startdate.year)
返回该错误
File "deconflict.py", line 29, in <module>
startdate = datetime.fromtimestamp(mktime(startdate))
AttributeError: 'module' object has no attribute 'fromtimestamp'
从文档(https://docs.python.org/2/library/datetime.html?highlight=datetime#module-datetime),datetime模块的日期时间对象有方法fromtimestamp,但进口并没有让我用它。
另一方面,使用从模块导入*修复所有问题。虽然我不明白为什么从时间导入*允许我只使用strptime(),但从datetime导入*我仍然必须说datetime.fromtimestamp。
import csv
from time import *
from datetime import *
startdate = strptime('31-Dec-50', "%d-%b-%y")
enddate = strptime('1-Jan-00', "%d-%b-%y")
with open('Classroom Utilization.csv', 'rb') as csvfile:
file = csv.DictReader(csvfile, delimiter=',')
for row in file:
checkstartdate = strptime(row['startdate'], "%d-%b-%y")
checkenddate = strptime(row['enddate'], "%d-%b-%y")
if checkstartdate < startdate:
startdate = checkstartdate
if checkenddate > enddate:
enddate = checkenddate
print strftime("%d-%b-%y", startdate)
print strftime("%d-%b-%y", enddate)
print "pre convert: " + str(startdate)
print "pre convert: " + str(enddate)
startdate = datetime.fromtimestamp(mktime(startdate))
enddate = datetime.fromtimestamp(mktime(enddate))
print "post convert: " + str(startdate)
print "post convert: " + str(enddate)
print '%s/%s/%s' % (startdate.month, startdate.day , startdate.year)