[python] CSV read and write using module xlrd and xlwt

1. get data from csv, skip header of the file.

1 with open('test_data.csv','rb,) as csvfile:
2         readCSV = csv.reader(csvfile, delimiter= ',')
3         headers = next(readCSV)
4         for row in readCSV:
5             distance_travelled.append(row[DISTANCE_COLUM_NO])

 2. get one colum of data

1 import csv
2 with open('A.csv','rb') as csvfile:
3     reader = csv.reader(csvfile)
4     column = [row[2] for row in reader]
5 print column

xlwt module

1. install, using the command :

pip install xlwt

 2. a quick start (from the internet)

import xlwt
from datetime import datetime

style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
    num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')

ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('example.xls')
原文地址:https://www.cnblogs.com/sarah-zhang/p/5451854.html