You could use a try/except
block:
try:
timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')
except ValueError:
timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
More Related Contents:
- Convert DataFrame column type from string to datetime
- Python datetime to string without microsecond component
- How to specify date format when using pandas.to_csv?
- Creating graph with date and time in axis labels with matplotlib
- Convert a column of timestamps into periods in pandas
- Using module ‘subprocess’ with timeout
- Python: Justifying NumPy array
- python list in sql query as parameter [duplicate]
- How to make a class property? [duplicate]
- How to get week number in Python?
- Don’t wait for a page to load using Selenium in Python
- Deep-Learning Nan loss reasons
- Scrapy and proxies
- matplotlib color gradient in patches?
- Faster alternatives to numpy.argmax/argmin which is slow
- Where is pip cache folder?
- fitting data with numpy
- How to change the path of Python in Spyder?
- Using cookies.txt file with Python Requests
- Terminate a python script from another python script
- Using FFmpeg and IPython
- Why are some mysql connections selecting old data the mysql database after a delete + insert?
- Can sphinx link to documents that are not located in directories below the root document?
- python – Finding the user’s “Downloads” folder
- How to access current location of any user using python [closed]
- RegEx with multiple groups?
- group by week in pandas
- Setupterm could not find terminal, in Python program using curses
- Extract csv file specific columns to list in Python
- Fit a non-linear function to data/observations with pyMCMC/pyMC