Python CSV 文件读写

原文地址

之前也曾使用过 python 处理过 csv 文件,就是普通的文本文件读写,不曾想到 Python 也内置了一个 csv 的库:

http://docs.python.org/release/2.5.4/lib/module-csv.html

没有细看,有空再看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import csv
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
print row
reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
for row in reader:
print row
writer = csv.writer(open("some.csv", "wb"))
writer.writerows(someiterable)
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
reader = csv.reader(open("passwd", "rb"), 'unixpwd')
filename = "some.csv"
reader = csv.reader(open(filename, "rb"))
try:
for row in reader:
print row
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
for row in csv.reader(['one,two,three']):
print row

OS Mountain Lion 10.8.2 安装 Cython

原文

使用 Python 开发的程序,运行速度上不太满意,碰巧看到赖勇浩 CSDN 博客中翻译的 Cython 三分钟入门。得空便开始着手尝试,半天未果。
问题一大堆,比如:

  • OS 下设置环境变量;
  • Clang(/klæŋ/)安装;

OS 下终端里设置临时环境变量的方法比较容易:

1
export PATH=$PATH:'app/bin'

OS设置永久环境变量,网上搜了一篇,没太搞明白,但起作用了:Mac 下设置环境变量

编译 Clang 需要 GCC,GCC 我在 XCode 安装目录下找到了:

1
/Applications/Xcode.app/Contents/Developer/usr/bin

按照《结构化编译器前端 Clang介绍》折腾半天,最终也没能编译成功。

回头再去XCode安装目录:

1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

找到了,果断复制了 Clang 至:

1
/Applications/Xcode.app/Contents/Developer/usr/bin

也不知道,合不合适,但终端里输入 Clang 终于有反应了

1
2
cd Cython-0.17.3
python setup.py build

阻力继续:

1
2
3
4
5
6
7
8
9
10
running build_py
running build_ext
building 'Cython.Plex.Scanners' extension
clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c Cython/Plex/Scanners.c -o build/temp.macosx-10.8-intel-2.7/Cython/Plex/Scanners.o
clang: warning: argument unused during compilation: '-mno-fused-madd'
Cython/Plex/Scanners.c:4:10: fatal error: 'Python.h' file not found
#include "Python.h"
^
1 error generated.
error: command 'clang' failed with exit status 1

stackoverflow 上有个最佳答案:

1
sudo apt-get install python-dev

mac 里没有 apt-get 这个工具,傻眼。。。

其实还在 Xcode 目录中:

1
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Python.framework/Versions/2.7/include

找到了“Python.h”,怎么让 python setup.py build 时知道呢?再傻眼。。。

没有师傅指导。。。

难免走弯路。。。

会遇到些稀奇古怪的东西,完全搞不清状况。。。

我看到了这篇文章:《Fatal Error:studio.h File Not Found》

“Command Line Tools” 的 comment:

“Before installing, note that from within Terminal you can use the XCRUN tool to launch compilers and other tools embedded within the Xcode application. Use the XCODE-SELECT tool to define which version of Xcode is active. Type “man xcrun” from within Terminal to find out more.

Downloading this package will install copies of the core command line tools and system headers into system folders, including the LLVM compiler, linker, and build tools.”

再次打开终端:输入 gcc,输入 clang,就这么简单,绕这么大弯。

python setup.py build 有些警告

1
sudo python setup.py install

结束!

Python 运算符重载

原文地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Line:
def __init__(self,p1,p2):
self.start = p1
self.end = p2
def __sub__(self,p):
if isinstance(p,Point):
if p is self.start:
return self.end
if p is self.end:
return self.start
class Point:
def __init__(self,x,y):
self.x = x
self.x = x
def __add__(self,p):
if isinstance(p,Point):
return Line(self,p)
if __name__ == '__main__':
p1 = Point(1,2)
p2 = Point(2,3)
#两个点相加 通过__add__方法
line = p1 + p2
print l
#>><__main__.Line instance at 0x02220738>
#线减去一个点 通过__sub__方法
print line - p1
#>><__main__.Point instance at 0x02220710>

Chrome 扩展

原文地址

再2分钟,11:30

10分钟前,我给自己找了件事情做——开发一个Chrome插件——Google搜索结果页上的链接在新Tab直接打开,而不经Google重定向!因为经Google重定向基本上会被墙,所以…

我正在下载”文明5:众神与国王”,现在它占用大量带宽,5分钟后下载完成,我开始阅读Chrome插件开发教程…

11:44
网上搜索的教程不靠谱!果断搜索官方教程!

11:50
我还没找到官方教程!!!!

11:51
https://developer.chrome.com/extensions/getstarted.html

12:00
Hello World!

13:09
有眉目了,遇到个问题jquery加载的问题…
任务暂停,去找孩他娘吃饭…

15:10
吃了花溪王回来,13点时,Javascript文件引入顺序错误,导致调用”$”失败!
目标功能已经实现了!

代码:

Mission Completed!——Dirty&Quick