標籤:
1 # -*- coding:utf-8 -*- 2 ‘‘‘ 3 Created on 2015年4月20日 4 5 @author: liuxue 6 7 ‘‘‘ 8 import csv 9 import sys10 reload(sys)11 sys.setdefaultencoding(‘utf-8‘)12 ‘‘‘13 valueList=[[]],雙層列表,每一個元素代表一行資料14 ‘‘‘15 16 def csvWrite(fileName="",mode="",valueList=[]):17 csvFile=file(fileName,mode)18 cstWriter=csv.writer(csvFile)19 for i in range(len(valueList)):20 temp=[]21 temp=valueList[i]22 cstWriter.writerow(temp)23 csvFile.close()24 25 def csvRead(fileName=""):26 csvFile=open(fileName,‘r‘)27 line = csvFile.readline().strip(‘\r\n‘)#如果有標題不想讀取,可以重複這句28 valueList=[]29 while(line!=""):30 temp=line.split(‘,‘)31 valueList.append(temp)32 line = csvFile.readline().strip(‘\r\n‘)33 csvFile.close()34 return valueList35 36 37 def main():38 valueList=[["我","d"]]39 csvWrite("test.csv","wb",valueList)40 value=csvRead("test.csv")41 for i in range(len(value)):42 temp=value[i]43 print temp44 for j in range(len(temp)):45 print temp[j]46 47 48 if __name__=="__main__":49 main()50 51 ‘‘‘52 輸出:53 [‘\xe6\x88\x91‘, ‘d‘]54 我55 d56 ‘‘‘
Python檔案之----CSV