之前也曾使用过python处理过csv文件,就是普通的文本文件读写,不曾想到Python也内置了一个csv的库:
http://docs.python.org/release/2.5.4/lib/module-csv.html
没有细看,有空再看
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