파이썬으로 크롤링을 하다보면 날짜 형태가 아주 특이한 경우가 있다. 그런 경우 어떻게 날짜 형식을 바꿔야할지 알아보자
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
이곳에 가보면 날짜 형식에 대한 모든 것이 나온다. 위 링크를 참조해서 데이터 형식에 맞춰 데이타를 받으면 된다. 저걸 제대로 못맞추면 에러난다.
'파이썬' 카테고리의 다른 글
Mecab에 새로운 단어 등록하기 (0) | 2020.11.17 |
---|---|
우분투 xdpyinfo 설치와 chromium 설치 그외 파이썬 크롤링 필수 설치 (0) | 2019.12.16 |
파이썬 백그라운드 실행 (0) | 2019.08.12 |
파이썬으로 카프카 토픽 가져와서 블록체인에 전송하고 txid 받아서 디비에 넣기 (0) | 2019.08.01 |
파이썬으로 스마트컨트랙트 배포하기 feat POA 2/2 (0) | 2019.07.23 |