顯示具有 Python 標籤的文章。 顯示所有文章
顯示具有 Python 標籤的文章。 顯示所有文章

2015年3月3日 星期二

How to convert a file to utf-8 in Python?

import codecs
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with codecs.open(sourceFileName, "r", "utf-16") as sourceFile:
    with codecs.open(targetFileName, "w", "utf-8") as targetFile:
        while True:
            contents = sourceFile.read(BLOCKSIZE)
            if not contents:
                break
            targetFile.write(contents)

Reference:http://stackoverflow.com/questions/4166070/python-csv-error-line-contains-null-byte

2014年10月22日 星期三

Python, Unicode, UTF-8, and str

For example:


tree = ET.ElementTree(file=temp_Path + 'netviewraw.xml')

for elem in tree.iter():
    if elem.text is not None:     #elem.text type is unicode
        elem.text.encode('utf8')  # encode to utf8
        cfg.append(elem.text.encode('utf8')) # cfg is a list
f = open(fn, 'w') # no need to set encoding='utf-8', because on python2.7, file read/write only support str

for text in cfg:
        f.write(text+ '\n') #
f.close()

Install PyWin32 correctly on Windows


  1. Download properly PyWin32 version for your OS version(32, 64 bit).
  2. Download correct python for PyWin32.
  3. Install python and set python path in environment variable. For example: python-2.7.3.amd64.msi
  4. Install PyWin32. For example: pywin32-219.win-amd64-py2.7.exe
  5. Open command line and change dir to C:\Python27\Scripts
  6. Enter following command: python pywin32_postinstall.py -install
     The most important step is step6.

2014年6月7日 星期六

How do I get the path of the current executed file in python?





Reference:
http://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python



Following are for copy&paste:
#
some_path/module_locator.py:
def we_are_frozen():
    # All of the modules are built-in to the interpreter, e.g., by py2exe
    return hasattr(sys, "frozen")

def module_path():
    encoding = sys.getfilesystemencoding()
    if we_are_frozen():
        return os.path.dirname(unicode(sys.executable, encoding))
    return os.path.dirname(unicode(__file__, encoding))
some_path/main.py:
import module_locator
my_path = module_locator.module_path()

2014年5月28日 星期三

Python, How to install library for portable python by using distribute on windows

For example:

1. Download distrubute
2. Unzip distribute, ex: "distribute-0.7.3"
3. Move "distribute-0.7.3" to "D:\PortablePython" (That is my portable python location).
4. Open command line
5. Enter following command without quotes:
    - "d:"
    - "cd "PortablePython"
    -  "Python-Portable.exe distribute-0.7.3\setup.py install"

If you want install package "pygal", just enter following command without quotes:
- "cd D:\PortablePython\App\Scripts"
- "easy_install-2.7.exe pygal"

==============================================================
Updated, another way:

Using "pip":
https://pip.pypa.io/en/latest/installing.html
http://www.codedata.com.tw/python/python-tutorial-the-1st-class-3-hello-world-traditional-chinese-edition