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

时间:2023-03-09 10:04:47
[python] CSV read and write using module xlrd and xlwt

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

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

2. get one colum of data

 import csv
with open('A.csv','rb') as csvfile:
reader = csv.reader(csvfile)
column = [row[2] for row in reader]
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')