1.读取CSV文件到List
def readCSV2List(filePath):
try:
file=open(filePath,'r',encoding="gbk")
context = file.read()
list_result=context.split("\n")
length=len(list_result)
for i in range(length):
list_result[i]=list_result[i].split(",")
return list_result
except Exception :
print("文件读取转换失败,请检查文件路径及文件编码是否正确")
finally:
file.close();
2.将List写入到CSV文件中
def writeList2CSV(myList,filePath):
try:
file=open(filePath,'w')
for items in myList:
for item in items:
file.write(item)
file.write(",")
file.write("\n")
except Exception :
print("数据写入失败,请检查文件路径及文件编码是否正确")
finally:
file.close();