반응형

파이썬으로 크롤링을 하다보면 날짜 형태가 아주 특이한 경우가 있다. 그런 경우 어떻게 날짜 형식을 바꿔야할지 알아보자

 

dt = "2019-12-03T16:32:48.0+0900"

 

이런식의 날짜를 받는 경우 이대로는 mysql에 date 타입으로 인서트가 안된다...

 

이럴때 mysql에 맞게 들어가도록 바꿔보자...

 

gameDate=datetime.datetime.strptime(dt,'%Y-%m-%dT%H:%M:%S.0%z')

 

이런식으로 형식에 맞게 일단 받는다...이렇게 받아서...

 

gameDate=str(gameDate.strftime('%Y-%m-%d %H:%M:%S'))

 

이렇게 다시 형식을 바꿔서 mysql에 입력하면 문제가 없다...

 

https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

 

8.1. datetime — Basic date and time types — Python 2.7.17 documentation

8.1. datetime — Basic date and time types The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extracti

docs.python.org

 

이곳에 가보면 날짜 형식에 대한 모든 것이 나온다. 위 링크를 참조해서 데이터 형식에 맞춰 데이타를 받으면 된다. 저걸 제대로 못맞추면 에러난다.

 

 

반응형

+ Recent posts