Reputation: 3640
This is the code I currently have:
security = input('Security: $')
print ('Date format: YEAR,MO,DA')
s = input('Start date: ')
e = input('End date: ')
start = dt.datetime(s)
end = dt.datetime(e)
df = web.DataReader(security, 'google', start, end)
print (df.tail())
How do I properly get user date input and be able to convert it? The error I am currently getting running this code is:
Traceback (most recent call last):
File "C:\Users\BregmanM\Documents\Python\PDF to Excel\StockPerformance\stockcompetitoranalyis.py", line 17, in <module>
start = dt.datetime(s)
TypeError: an integer is required (got type str)
I understand the error is for wrong type, but I am trying to make this a more efficient. How should I go about doing this the right way?
Upvotes: 0
Views: 116
Reputation: 21643
We are the most perverse species on the planet. It can be as well to take that into account. And dates are wicked. If the user doesn't put the commas in we can detect that and inform them. However, once they've done that we should be able to provide some support, assuming that they can get the order right. Here's a bit of code that makes an attempt and displays its results.
First it looks for items delimited by commas, allowing for random spaces. The user is permitted to enter two- or four-character centuries, numerical or character months and single- or two-character numerical days. By making simple judgments from the tidbits the code constructs a format for use by the chrome
library in obtaining a date. This library is capable of making its dates available in internal formats that are interchangeable with standard libraries.
import arrow
import re
def process_user_date(user_date):
m = re.match(r'(\d{,4})\s*\,\s*(\w{,8})\s*\,\s*(\d{,2})', user_date)
if not m:
raise ValueError('Need date as year number [comma] month name or number [comma] day number')
print (m.groups())
year_length = len(m.groups()[0])
if not year_length in [2, 4]:
raise ValueError('Year number must be either two or four digits long')
if m.groups()[1].isalpha() and len(m.groups()[1]) < 3:
raise ValueError('Month name must be at least three characters')
month_length = len(m.groups()[1])
if m.groups()[1].isnumeric() and month_length > 2:
raise ValueError('Month number cannot be longer that two characters')
month_length = min(4, month_length)
day_length = len(m.groups()[2])
if not day_length in [1, 2]:
raise ValueError('Day number must be either one or two digits long')
return arrow.get('%s%s%s' % m.groups(), '%s%s%s' % (year_length*'Y', month_length*'M', day_length*'D'))
print (process_user_date('2017 ,November , 7'))
print (process_user_date('2017 ,Nov , 7'))
print (process_user_date('2017 ,November , 07'))
print (process_user_date('2017 ,11 , 07'))
print (process_user_date('17 ,November , 7'))
print (process_user_date('2017 ,11, 7'))
print (process_user_date('17 ,11 , 07'))
Results:
('2017', 'November', '7')
2017-11-07T00:00:00+00:00
('2017', 'Nov', '7')
2017-11-07T00:00:00+00:00
('2017', 'November', '07')
2017-11-07T00:00:00+00:00
('2017', '11', '07')
2017-11-07T00:00:00+00:00
('17', 'November', '7')
2017-11-07T00:00:00+00:00
('2017', '11', '7')
2017-11-07T00:00:00+00:00
('17', '11', '07')
2017-11-07T00:00:00+00:00
Upvotes: 1