import operator
alist=[[],[],[],[]]
alist.sort(key=operator.itemgetter(0,1,2))
import os
path = "D:/Python34/news" #文件夹目录
files= os.listdir(path) #得到文件夹下的所有文件名称
for file in files: #遍历文件夹
if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开
f = open(path+"/"+file); #打开文件
import csv
with open('A.csv','rb') as csvfile:
reader = csv.reader(csvfile)
rows= [row for row in reader]
print rows
得到如:
[[‘No.’, ‘Name’, ‘Age’, ‘Score’],
[‘1’, ‘Apple’, ‘12’, ‘98’],
[‘2’, ‘Ben’, ‘13’, ‘97’],
[‘3’, ‘Celia’, ‘14’, ‘96’],
[‘4’, ‘Dave’, ‘15’, ‘95’]]
1、将一个二重列表[[],[]]写入到csv文件中
2、从文本文件中读取返回为列表
3、将一字典写入到csv文件中
4、从csv文件中读取一个字典
5、从csv文件中读取一个计数字典
https://www.cnblogs.com/moye13/p/5291156.html
# 功能:将一个二重列表写入到csv文件中
# 输入:文件名称,数据列表
def createListCSV(fileName="", dataList=[]):
with open(fileName, "wb") as csvFile:
csvWriter = csv.writer(csvFile)
for data in dataList:
csvWriter.writerow(data)
csvFile.close
# 功能:从文本文件中读取返回为列表的形式
# 输入:文件名称,分隔符(默认,)
def readListCSV(fileName="", splitsymbol=","):
dataList = []
with open(fileName, "r") as csvFile:
dataLine = csvFile.readline().strip("\n")
while dataLine != "":
tmpList = dataLine.split(splitsymbol)
dataList.append(tmpList)
dataLine = csvFile.readline().strip("\n")
csvFile.close()
return dataList
# 功能:将一字典写入到csv文件中
# 输入:文件名称,数据字典
def createDictCSV(fileName="", dataDict={}):
with open(fileName, "wb") as csvFile:
csvWriter = csv.writer(csvFile)
for k,v in dataDict.iteritems():
csvWriter.writerow([k,v])
csvFile.close()
# 功能:从csv文件中读取一个字典
# 输入:文件名称,keyIndex,valueIndex
def readDictCSV(fileName="", keyIndex=0, valueIndex=1):
dataDict = {}
with open(fileName, "r") as csvFile:
dataLine = csvFile.readline().strip("\n")
while dataLine != "":
tmpList = dataLine.split(splitsymbol)
dataDict[tmpList[keyIndex]] = tmpList[valueIndex]
dataLine = csvFile.readline().strip("\n")
csvFile.close()
return dataDict
# 功能:从csv文件中读取一个计数字典
# 输入:文件名称,keyIndex
def readDictCSV(fileName="", keyIndex=0):
dataDict = {}
with open(fileName, "r") as csvFile:
dataLine = csvFile.readline().strip("\n")
while dataLine != "":
tmpList = dataLine.split(splitsymbol)
if dataDict.get(tmpList[keyIndex]) == None:
dataDict[tmpList[keyIndex]] = 0
dataDict[tmpList[keyIndex]] = dataDict.get(tmpList[keyIndex]) + 1
dataLine = csvFile.readline().strip("\n")
csvFile.close()
return dataDict