2016-05-29 91 views
0

难以置信地围绕着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) 

回答

2

在此特定情况下,datetime模块具有datetime类。这很令人困惑,因为它们具有相同的名称。当你做import datetime时,你会得到一个名为datetime的模块。要访问该模块的成员(如datetime类),你需要完全限定它(例如:datetime.datetime

例如:

import datetime 
startdate = datetime.datetime.fromtimestamp(mktime(startdate)) 

当你from datetime import *,由datetime引用的就是不是模块,而是同名的类。你得到相同的对象,如果你没有from datetime import datetime,这意味着“从日期时间模块导入日期时间

1

datetime模块之内具有DateTime类。这是你将如何使用它

from datetime import * 
datetime.datetime.fromtimestamp 

from datetime import datetime 
datetime.fromtimestamp 

时模块没有时间类直接公开它的方法。