用python寫CSV、EXCEL文件
阿新 • • 發佈:2017-08-30
() import exce 讀取 key print tput save style
import pandas as pd
writer = pd.ExcelWriter(‘output.xlsx‘)
df1 = pd.DataFrame(data={‘col1‘:[1,1], ‘col2‘:[2,2]})
df1.to_excel(writer,‘Sheet1‘)
writer.save()
http://www.cnblogs.com/liutongqing/p/6892099.html
import csv
# 讀取csv文件方式1
csvFile = open("csvData.csv", "r")
reader = csv.reader(csvFile) # 返回的是叠代類型
data = []
for item in reader:
print(item)
data.append(item)
print(data)
csvFile.close()
# 讀取csv文件方式2
with open("csvData.csv", "r") as csvfile:
reader2 = csv.reader(csvfile) # 讀取csv文件,返回的是叠代類型
for item2 in reader2:
print(item2)
csvFile.close()
# 從列表寫入csv文件
csvFile2 = open(‘ csvFile2.csv‘,‘w‘, newline=‘‘) # 設置newline,否則兩行之間會空一行
writer = csv.writer(csvFile2)
m = len(data)
for i in range(m):
writer.writerow(data[i])
csvFile2.close()
# 從字典寫入csv文件
dic = {‘張三‘:123, ‘李四‘:456, ‘王二娃‘:789}
csvFile3 = open(‘csvFile3.csv‘,‘w‘, newline=‘‘)
writer2 = csv.writer(csvFile3)
for key in dic:
writer2.writerow([key, dic[key]])
csvFile3.close()
用python寫CSV、EXCEL文件