https://stackoverflow.com/questions/5757743/how-can-i-get-a-specific-field-of-a-csv-file
港股即時
Economic Calendar
Thursday, October 12, 2017
Friday, October 6, 2017
Python Property
https://www.programiz.com/python-programming/property
__STR__ & __REPR__
http://mrlyc.blogspot.hk/2014/04/pythonreprstr.html
Wednesday, October 4, 2017
Import vs from
Import X :
From X import * :
- imports the module X, and creates a reference to that module in the current namespace. Then you need to define completed module path to access a particular attribute or method from inside the module.
- For example: X.name or X.attribute
From X import * :
- * imports the module X, and creates references to all public objects defined by that module in the current namespace (that is, everything that doesn’t have a name starting with “_”) or what ever the name you mentioned. Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.* This makes all names from the module available in the local namespace.
Now let’s see when we do import X.Y:
>>> import sys
>>> import os.path
Check sys.modules with name os and os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
Check globals() and locals() namespace dict with name os and os.path:
>>> globals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> locals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> globals()['os.path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os.path'
>>>
From the above example we found that only os is inserted in the local and global namespace. So, we should be able to use:
>>> os
<module 'os' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> os.path
<module 'posixpath' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
But not path
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Once you delete the os from locals() namespace, you won't be able to access os as well as os.path even though they exist in sys.modules:
>>> del locals()['os']
>>> os
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Now let's come to from :
** from :**
>>> import sys
>>> from os import path
Check sys.modules with name os and os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
Oh, we found that in sys.modules we found as same as we did before by using import name
OK, let's check how it looks like in locals() and globals() namespace dict:
>>> globals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> locals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['os']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os'
>>>
You can access by using name path not by os.path:
>>> path
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Let's delete 'path' from locals():
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
One final example using an alias:
>>> from os import path as HELL_BOY
>>> locals()['HELL_BOY']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['HELL_BOY']
<module 'posixpath' from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
And no path defined:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
Sunday, October 1, 2017
Python csv.writerow寫入多隔一行
用writerow 寫入時,每row 資料之間會多隔一行的情況。
原因是,在Windows系統下會幫每一行結尾多加一個看不見的"進位符號",然而這個動作writerow本身就會幫我們做,所以等於重複按Enter兩次。避免這種狀況一般常見的解決方法是以binary的方式開啟檔案:
f = open("xxx.csv","wb")
c = csv.writer(f)
然而這種方法在Python 3 下會產生錯誤:
ValueError: binary mode doesn't take a newline argument
在python 3的解決方法是,在後面多加一個參數
f = open('xxx.csv', 'w', newline='')
If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.
http://pykynix.blogspot.hk/2013/01/csvwriterow.html
原因是,在Windows系統下會幫每一行結尾多加一個看不見的"進位符號",然而這個動作writerow本身就會幫我們做,所以等於重複按Enter兩次。避免這種狀況一般常見的解決方法是以binary的方式開啟檔案:
f = open("xxx.csv","wb")
c = csv.writer(f)
然而這種方法在Python 3 下會產生錯誤:
ValueError: binary mode doesn't take a newline argument
在python 3的解決方法是,在後面多加一個參數
f = open('xxx.csv', 'w', newline='')
If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.
http://pykynix.blogspot.hk/2013/01/csvwriterow.html
Subscribe to:
Comments (Atom)