2013-10-18 7 views
-3

제발 당신의 도움이 필요합니다. 지정된 날짜에 해당하는 엑셀 날짜의 일부 방정식을 구현하기 위해 calcExcelDate라는 이름으로 정의 된 함수를 사용하는 PYTHON 코드를 작성하려고합니다.정의 된 함수를 사용하여 일부 방정식 구현하기

  • 년 (1900 ~ 9999의 4 자리 정수).
  • 월 (1에서 12까지의 1 또는 2 자리 정수);
  • 일 (월에 따라 1 ~ 31 사이의 1 또는 2의 정수).

위의 주어진 연도, 월 및 일의 범위를 지정하는 루프에 3을 사용했습니다. 지금까지

y_2 = year - 1900 
em = math.floor((14 - month)/12) 
y_3 = y_2 - em 
m_2 = month + 12 * em 
I_ = 1 + min(y_3, 0) + math.floor(y_3/4) - math.floor(y_3/100) + math.floor((y_3 + 300)/400) 
d_1 = math.floor(-1.63 + (m_2 - 1) * 30.6) 
d_2 = day + y_3 * 365 + I_ + d_1 

내 코드 : 그러나 때라도 방정식을 구현하기 위해 해당 기능을 사용할 수있는 방법을

이런 식으로 뭔가가에 대한 인터페이스 표준 라이브러리 datetime를 사용하는 것이 좋을 것이다 들어
import time 
import math 


DAYS = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") 
FULL_MONTHS = ("January", "February", "March", "April", "May", "June", 
"July", "August", "September", "October", "November", "December") 


mm_ = tuple(str(date) for date in range(2013-10-17, 2100-01-01)) # a tuple of character strings containing dates in the date range Excel supports, 
                    # such as ("2013-Oct-17", "2100-01-01") 
print mm_ 


def calcExcelDate(year, month, day): 

    for year in range(1900, 10000): 
    for month in range(1, 13): 
     for day in range(1, 32): 
     y_2 = year - 1900 
     em = math.floor((14 - month)/12) 
     y_3 = y_2 - em 
     m_2 = month + 12 * em 
     I_ = 1 + min(y_3, 0) + math.floor(y_3/4) - math.floor(y_3/100) 
          + math.floor((y_3 + 300)/400) 
     d_1 = math.floor(-1.63 + (m_2 - 1) * 30.6) 
     d_2 = day + y_3 * 365 + I_ + d_1 


xx_ = calcExcelDate(2010, 10, 1) 
print xx_ 
+1

(2010 , 10,1)'? 나는 당신이 모든'year' /'month' /'day'를 순환 할 필요가 없다고 생각한다. (특히 당신이 함수에 전달한'year /'month' /'day'를 덮어 쓰기 때문에). 그래서 세 개의 for 루프를 삭제하는 것은 아마도 첫 번째 단계 일뿐입니다. 방정식은 그대로 두지 만 반환 할 항목을 실제로 알지 못합니다. 'd_2'에 원하는 최종 답변이 포함되어 있습니까? –

+0

동의. "함수"라는 단어는 일반적으로 입력과 출력 사이의 관계를 의미합니다. 입력이 날짜가되는 것 같습니다. 원하는 출력은 무엇입니까? 코드가 원하는 값을 계산하는지 여부를 확인하기 위해 손으로 한 ** 구체적인 예 **는 무엇입니까? – dyoo

답변

0

날짜 및 시간을 처리 한 다음 필요에 따라 Excel 일련 날짜로 변환하십시오.

다음 코드는 XlsxWriter 모듈에서 datetime의 날짜가 Excel 날짜로 변환되는 방법을 보여줍니다.

def _convert_date_time(self, dt_obj): 
    # We handle datetime .datetime, .date and .time objects but convert 
    # them to datetime.datetime objects and process them in the same way. 
    if isinstance(dt_obj, datetime.datetime): 
     pass 
    elif isinstance(dt_obj, datetime.date): 
     dt_obj = datetime.datetime.fromordinal(dt_obj.toordinal()) 
    elif isinstance(dt_obj, datetime.time): 
     dt_obj = datetime.datetime.combine(self.epoch, dt_obj) 
    else: 
     raise TypeError("Unknown or unsupported datetime type") 

    # Convert a Python datetime.datetime value to an Excel date number. 
    delta = dt_obj - self.epoch 
    excel_time = (delta.days 
        + (float(delta.seconds) 
        + float(delta.microseconds)/1E6) 
       /(60 * 60 * 24)) 

    # Special case for datetime where time only has been specified and 
    # the default date of 1900-01-01 is used. 
    if dt_obj.isocalendar() == (1900, 1, 1): 
     excel_time -= 1 

    # Account for Excel erroneously treating 1900 as a leap year. 
    if not self.date_1904 and excel_time > 59: 
     excel_time += 1 

    return excel_time 

datetime을 사용하는 이점은 당신이 날짜 또는 시간으로 구문 분석 문자열에 대한 방법의 다양한 표준 인터페이스를 가지고있다 :

my_datetime = datetime.datetime.strptime('2013-01-23', '%Y-%m-%d') 
정확히 calcExcelDate`에서 출력으로 기대하고 무엇
관련 문제