读写excel数据方法
- 首选pandas库里
pandas.read_excel
和pandas.to_excel
函数,相对比较简单.
"""
Created on Wed Nov 28 18:39:17 2018
@author: **
"""
import xlrd
import xlwt
import pandas
import numpy as np
def pandas_parse_xls(filename, imgname_col_index = [1], sub_index = None):
"""
reference:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html#pandas.read_excel
func:
parse the xlsx file into the ndarray or list
args:
imgname_col_index:, list of int, refer the col index of image name field
mos_col_index: list of int, refer the col index of MOS field
std_index: list of ints, refer the col index of subjetive score area
return:
list of imgname, MOS and std_value
"""
if sub_index is None:
Sub_score_area = range(3,15)
imgname_list = pandas.read_excel(filename, parse_cols = imgname_col_index).astype('str').values
imgname_list = np.squeeze(imgname_list).tolist()
sub_DataFrame = pandas.read_excel(filename, parse_cols = Sub_score_area)
sub_array = sub_DataFrame.values
std_array = np.std(sub_array,axis=1,ddof=1)
return imgname_list, std_array
def pandas_write_excel(filename, imgname_col_index = [0]):
imgname_DataFrame = pandas.read_excel(filename, parse_cols = imgname_col_index).astype('str').values
imgname_Arr = np.squeeze(imgname_DataFrame)
result = []
for item in imgname_Arr:
if not item.endswith('.jpg'):
item = item +'.jpg'
result.append(item)
img_DataFrame = pandas.DataFrame(data = result, columns = ['imagename'])
writer = pandas.ExcelWriter(filename)
img_DataFrame.to_excel(writer,index = False)
writer.save()
def xlrd_parse_xls(path_xls):
"""
reference: https://xlrd.readthedocs.io/en/latest/api.html
func:
#Cell object in the given row and column.
xlrd.sheet.cell(rowx,colx)
#Value of the cell in the given row,column.
xlrd.sheet.cell_value(rowx,colx)
#Returns a slice of the values of the cells in the given column.
xlrd.sheet.col_values(index_col) # the first index value is 1
#Returns a slice of the values of the cells in the given row.
xlrd.sheet.row_values(index_col)
"""
data = xlrd.open_workbook(path_xls)
table = data.sheets()[0]
name_list = [item.encode('utf-8') for item in table.col_values(1,1)]
score_list = table.col_values(15,1)
return name_list,score_list
def write_xls(dest_xls):
work_book = xlwt.Workbook(encoding = 'ascii')
work_sheet = work_book.add_sheet('sheet1')
work_sheet.write(0, 0, label = 'Row 0, Column 0 Value')
work_book.save('Excel_Workbook.xls')
if __name__=='__main__':
pandas_parse_xls('MOS.xlsx')
xlrd_parse_xls('MOS.xlsx')
读写csv数据方法
def read_csv(input_csv_path):
f = open(input_csv_path, 'r', newline='', encoding='utf-8')
readerObject = csv.reader(f)
for index, row in enumerate(readerObject):
if index == 0:
continue
count += 1
image_name = os.path.join(input_test_dir, row[0])
pts = row[1:-1]
print(pts)
print(count)
f.close()
def write_csv():
f = open('csv_path.csv', 'w', encoding='utf-8', newline='')
field_names = ['imageName', 'x0', 'y0', 'x0', 'y1','appid', 'rows', 'cols']
writer = csv.writer(f, dialect='excel')
writer.writerow(field_names)
wm_rows = 2
wm_cols = 3
for item in os.listdir(input_test_dir):
crop_name = os.path.splitext(item)[0]+".png"
crop_png_path = os.path.join(crop_dir, crop_name)
if item.endswith(".JPG") or item.endswith(".jpg") or item.endswith(".png"):
img_name = os.path.join(input_test_dir, item)
pts = []
pts.append(appid)
pts.append(wm_rows)
pts.append(wm_cols)
pts.insert(0,img_name)
writer.writerow(pts)
f.close()
import os
import json
import csv
import pandas
import shutil
import numpy as np
def get_csv_data(input_csv_path, input_csv_path1):
if not os.path.exists(input_csv_path):
raise FileNotFoundError("%s not found" % input_csv_path)
data = pandas.read_csv(input_xls_path, header=None)
data1 = pandas.read_csv(input_xls_path, index_col=None)
df= pandas.read_csv(input_csv_path1)
for col in df.columns:
print('列名:', col)
print('列数据:', df[col].values)
for index, row in df.iterrows():
print('行名:', index)
print('行数据:', row.values)
reader = data.parse(sheet_name="Sheet1")
files = reader.get('image_name')
reader.set_index('image_name', inplace=True)
for name in files:
col_items = reader[reader.index == name]
pts = col_items['points'].values
uuid = col_items['uuid'].values
if pandas.isnull(pts) or pandas.isnull(uuid):
continue
print(name, '--->', pts, '---->', uuid[0])
def get_pts_xls(input_path, data_dir, save_dir):
if not os.path.exists(input_path):
raise FileNotFoundError("%s not found" % input_path)
if not os.path.exists(data_dir):
raise FileNotFoundError("%s not found" % data_dir)
data = pandas.ExcelFile(input_path)
reader = data.parse(sheet_name="Sheet1")
files = reader.get('fileName')
for index, row in reader.iterrows():
image_name = row[1]
pts = row[4:12]
tmp = [str(i) for i in pts.values]
ptsList = ','.join(tmp)
src = os.path.join(data_dir, image_name)
dst = os.path.join(save_dir, image_name)
homography_transfer_roi(src, ptsList, dst)
break
if __name__=="__main__":
get_pts_xls(r"./rec.xlsx", r"./data", r"./roi")
参考地址
- xlrd_API
- pandas.read_excel_API
- csv官方API