1. 程式人生 > >python -- read write excel

python -- read write excel

def _read_excel_file(in_file):
    trace_pos_arr = []
    data = xlrd.open_workbook(in_file)
    for table in data.sheets():
        nrows = table.nrows
        for i in range(nrows):
            if i == 0:
                continue
            row = table.row_values(i)
            # Convert all values to unicode.
# row = map(unicode, row) trace_id, trace_x, trace_y, trace_z = int(row[0]), int(row[1]), int(row[2]), int(row[3]) # yield (trace_id, trace_x, trace_y, trace_z) trace_pos_arr.append([trace_x, trace_y, trace_z]) return trace_pos_arr

def _write2excel
(traces, to_file): workbook = xlsxwriter.Workbook(to_file) sheet = workbook.add_worksheet() idx = 0 time = 0 idx = _write_row_in_sheet(idx, ["x", "y", "z"], sheet, time) for trace in traces: if trace is None: continue print(trace) print(len(trace)
) org_1 = trace[0][0] org_2 = trace[0][1] org_3 = trace[0][2] time = trace[0][3] row = [org_1, org_2, org_3] idx = _write_row_in_sheet(idx, row, sheet, time) workbook.close() def _write2excel2(traces, to_file): workbook = xlsxwriter.Workbook(to_file) sheet = workbook.add_worksheet() idx = 0 time = 0 idx = _write_row_in_sheet(idx, ["x", "y", "z"], sheet, time) for trace in traces: row = [] for i in range(len(trace)): org = trace[i] row.append(org) idx = _write_row_in_sheet(idx, row, sheet, time) workbook.close() def _write2excel3(traces, to_file): workbook = xlsxwriter.Workbook(to_file) sheet = workbook.add_worksheet() idx = 0 time = 0 idx = _write_row_in_sheet(idx, ["x", "y", "z"], sheet, time) for trace in traces: print(trace) idx = _write_row_in_sheet2(idx, trace, sheet, time) workbook.close() def _write_row_in_sheet2(row_index, trace, sheet, time, format=None): for j in range(len(trace)): row = trace[j][:3] time = trace[j][-1] for i, item in enumerate(row): sheet.write(row_index, time * 3 + 1 + i, round(item, 1), format) return row_index + 1 def _write_row_in_sheet(row_index, row, sheet, time, format=None): for i, item in enumerate(row): sheet.write(row_index, time * 3 + 1 + i, item, format) return row_index + 1