0
Pythonのインポートに頭を悩ませました。私はimport XとX import * here- http://effbot.org/zone/import-confusion.htmの違いを読んだが、import "module"と "module" import *の違いを理解するのは苦労している。特に、エフェボットの記事では、インポート "モジュール"を貼ることを推奨しているので、ここでは機能しません。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)
このエラーを返しドキュメント(https://docs.python.org/2/library/datetime.html?highlight=datetime#module-datetime)から
File "deconflict.py", line 29, in <module>
startdate = datetime.fromtimestamp(mktime(startdate))
AttributeError: 'module' object has no attribute 'fromtimestamp'
、datetimeモジュールでのdatetimeオブジェクトは、メソッドのfromtimestampを持っていますが、輸入は私を聞かせていませんこれを使って。
一方、from module import *を使用すると、すべての問題が修正されます。なぜ私はtime import *を使ってstrptime()を使うことができるのか、datetime importを使っているのか理解できませんが、私はまだdatetime.fromtimestampと言う必要があります。この特定の場合、datetime
クラスを有するdatetime
モジュールにおいて
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)