1 # -*- coding:utf-8 -*- 2 ‘‘‘ 3 Created on 2015年4月20日 4 5 @author: liuxue 6 7 ‘‘‘ 8 import csv 9 import sys 10 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 valueList 35 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 temp 44 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 d 56 ‘‘‘
时间: 2024-10-05 14:48:53