Ich habe mich schwer mit Python-Importen beschäftigt. Ich habe den Unterschied zwischen Import X und von X Import * hier- http://effbot.org/zone/import-confusion.htm gelesen, aber mir fällt es schwer, den Unterschied zwischen Import "Modul" und "Modul" Import * zu verstehen. Zumal der effbot-Artikel empfiehlt, beim Import mit "Modul" zu bleiben und das funktioniert hier nicht.Python Imports Explained
Dieser Code
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)
gibt diesen Fehler zurück
File "deconflict.py", line 29, in <module>
startdate = datetime.fromtimestamp(mktime(startdate))
AttributeError: 'module' object has no attribute 'fromtimestamp'
Aus der Dokumentation (https://docs.python.org/2/library/datetime.html?highlight=datetime#module-datetime), hat das Datetime-Objekt in dem Datetime-Modul, das Verfahren fromtimestamp, aber der Import mich nicht lassen benutze es.
Auf der anderen Seite behebt die Verwendung von Modul Import * alle Probleme. Ich verstehe zwar nicht, warum ich mit time time import * nur strptime() verwenden kann, aber mit datetime import * muss ich noch datetime.fromtimestamp angeben.
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)