My python code shown below is not retrieving any dates before the year 1900. I learned that this is a limitation of datetime.strptime().
I have tried to follow some of the workaround mentioned in similar posts , Is there any way to use a strftime-like function for dates before 1900 in Python? but they seem a bit complicated for me to follow. I also learned that there is a patch available to fix this issue. https://bugs.python.org/file10253/strftime-pre-1900.patch
I tried to install the patch in Pycharm by copying the patch to a text file, but I am getting the following error message. Any ideas on what I need to do to run the patch successfully to get dates before 1900 ?
Pycharm Patch Error Screenshot
My Code:
from datetime import datetime
import csv
with open('train.csv', 'r') as f_input, open('sample.txt', 'w') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
for row in csv_input:
for date_format in ['%Y']:
try:
converted = datetime.strptime(row[3], date_format)
csv_output.writerow([row[0], row[1], row[2], converted.strftime(date_format)])
except ValueError:
pass
I realize this isn't what you asked, but I'll put it out there anyway since I'm assuming that if the answers to the linked question are too complicated to follow, the idea to patch your problem away successfully is probably unrealistic.
The limitation you are seeing is present in Python2:
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
It is somewhat rectified in Python3.2, and fully rectified in Python3.3 and onward:
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
'1899-01-01'
Parsing works too:
>>> d = datetime.strptime('0113-01-01','%Y-%m-%d')
>>> d
datetime.datetime(113, 1, 1, 0, 0)
>>> d.isoformat()
'0113-01-01T00:00:00'
>>> d.strftime('%Y-%m-%d')
'113-01-01'
So if that is an acceptable option, you could switch over to Python3.3+ and you won't have this issue.
Related
I'm teaching myself Python and was just "exploring". Google says that datetime is a global variable but when I try to find todays date in the terminal I receive the NameError in the question title?
mynames-MacBook:pythonhard myname$ python
Enthought Canopy Python 2.7.3 | 64-bit | (default, Aug 8 2013, 05:37:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> date = datetime.date.today()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'datetime' is not defined
>>>
You need to import the module datetime first:
>>> import datetime
After that it works:
>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)
It can also be used as below:
from datetime import datetime
start_date = datetime(2016,3,1)
end_date = datetime(2016,3,10)
This is my project structure.
├─demo_data
├─findb
│ └─__pycache__
├─management
└─tw_equity
├─migrations
│ └─__pycache__
└─__pycache__
Now I try to insert demo data to my models.
So I create commands.pyfor the process.
# ./management/commands.py
from datetime import datetime
from tw_equity.models import (
TradingDate,
Basic,
Price,
Holder,
)
with open('demo_data/trading_date.csv') as f:
for row in f:
data = datetime.strptime(row, "%Y-%m-%d").date()
trading_date = TradingDate(
trading_date=data
)
trading_date.save()
print('Insert Successfully.')
But the error keeps showing up.
(Findb) C:\Users\ycyta\python-django-Findb>management\commands.py
Traceback (most recent call last):
File "C:\Users\ycyta\python-django-Findb\management\commands.py", line 2, in <module>
from tw_equity.models import (
ModuleNotFoundError: No module named 'tw_equity'
Already tried
python manage.py shell, it is all good.
(Findb) C:\Users\ycyta\python-django-Findb>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from tw_equity.models import TradingDate
>>> TradingDate
<class 'tw_equity.models.TradingDate'>
Mostly similar questions before were concerned about __ini__.py, but I already have.
Thanks for helping.
I tried multiple times to make this work without results, here's the code
import os, sys
try:
with open("syscore.lib", "r") as confFile:
readConfFile = confFile.readlines()
fileExist = True
except:
fileExist = False
if fileExist is True:
method = "r"
else:
method = "w"
for _ in range(2):
with open("syscore.lib", method) as confFile:
try:
readConfFile = confFile.readlines()
except:
confFile.write(os.system("python --version").replace("Python ", ""))
print(readConfFile)
The problem come again in a similar way down here
import os
test = [str(os.system('python --version'))]
test1 = os.system('python --version')
print('PV: '+str(test1))
print('Python Version: '+test[0])
Can anyone help me with this?
Thanks
As noted in the relevant thread here, os.system() returns the process exit value. 0 means success. If you want to capture the printed output, use os.popen() instead:
import os
python_version = os.popen("python -V").read().strip()
And the output is 'Python 3.6.6'.
Hope this helps.
if you only want to get python version then try,
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
>>> version = "{}.{}.{}".format(*sys.version_info)
'2.7.12'
>>>
you will get in python3,
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> "{}.{}.{}".format(*sys.version_info)
'3.5.2'
>>>
or you can use,
>>> import sys
>>> sys.version
'3.5.2 (default, Nov 12 2018, 13:43:14) \n[GCC 5.4.0 20160609]'
>>>
Is file a keyword in python?
I've seen some code using the keyword file just fine, while others have suggested not to use it and my editor is color coding it as a keyword.
No, file is not a keyword:
>>> import keyword
>>> keyword.iskeyword('file')
False
The name is not present in Python 3. In Python 2, file is a built-in:
>>> import __builtin__, sys
>>> hasattr(__builtin__, 'file')
True
>>> sys.version_info[:2]
(2, 7)
It can be seen as an alias for open(), but it was removed in Python 3, where the new io framework replaced it. Technically, it is the type of object returned by the Python 2 open() function.
file is neither a keyword nor a builtin in Python 3.
>>> import keyword
>>> 'file' in keyword.kwlist
False
>>> import builtins
>>> 'file' in dir(builtins)
False
file is also used as variable example from Python 3 doc.
with open('spam.txt', 'w') as file:
file.write('Spam and eggs!')
As others suggested, type in Python 3 it is not defined by default:
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> file
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
The color coding in VS Code and possibly other editors probably refers to Python 2, where it is defined by default, it is the type returned by open():
Python 2.7.18 (default, Jul 1 2022, 12:27:04)
[GCC 9.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> file
<type 'file'>
eduardo#camizao:/$ python2.7
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> url1 = 'http://www.google.com'
>>> url2 = 'https://www.google.com'
>>> f = urllib.urlopen(url1)
>>> f = urllib.urlopen(url2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib.py", line 87, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 211, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 355, in open_http
'got a bad status line', None)
IOError: ('http protocol error', 0, 'got a bad status line', None)
>>>
When I try to connect to an https site, using urllib I got error above.
Proxies are correctly setup. Debugging python code, I have noticed in urllib.py that an import on ssl library is not performed. So, https calls are also not performed. Can anyone help me, please? I do have to use urllib, instead of urllib2 or another one. Thanks in advance.
It's not something wrong with the way you are writing it at least:
$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> url1 = 'http://www.google.com'
>>> url2 = 'https://www.google.com'
>>> f = urllib.urlopen(url1)
>>> f = urllib.urlopen(url2)
>>> f.read()[:15]
'<!doctype html>'
>>>
So that's what it's not. It must be something with your environment or your config then. You said you are using proxies?
EDIT:
I'm able to open it via an open proxy (won't include said proxy because who knows if it is sketchy - substitute with your own proxy:
$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> proxy_handler = urllib2.ProxyHandler({'http': 'http://some-sketchy-open-proxy'})
>>> opener = urllib2.build_opener(proxy_handler)
>>> opener.open('https://www.google.com')
<addinfourl at 140512985881056 whose fp = <socket._fileobject object at 0x7fcbba9b1ed0>>
>>> _.read()[:15]
'<!doctype html>'
>>>
Try it that way (note that I used urllib2, not urllib) with your own proxy URL. Hope that helps!
EDIT 2:
Using only urllib:
$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import urllib
>>> proxies = {'http': '189.112.3.87:3128'}
>>> url = 'https://www.google.com'
>>> filehandle = urllib.urlopen(url,proxies=proxies)
>>> filehandle.read()[:15]
'<!doctype html>'
>>>