2014-12-11 2 views
-3

2 xls 파일의 값을 비교해야하는 코드를 작성했습니다. 파일 중 하나에 1 장 이상이 있고 마지막 시트에서만 항상 데이터를 읽어야합니다. 나는 이걸 어떻게 관리해야할지 모르겠다. 내가 그 코드와 그것을 해결하기 위해 관리python xlrd, 통합 문서의 마지막 시트에서 읽음

#! /usr/bin/python 
import xlrd #Import the package to read from excel 
#start with station report 
station_rep = xlrd.open_workbook("/home/fun/data/Station.xls",encoding_override='utf8') #Open the station report.xls 
station_sheet = station_rep.sheet_by_index(0) #should get the last sheet 
station_vn = station_sheet.col_values(5, start_rowx=1, end_rowx=None) #List of vouchers in station report 
#start with billing export 
billing_rep = xlrd.open_workbook("/home/fun/data/Export.xls",encoding_override='utf8') #Open billing report xls 
billing_sheet = billing_rep.sheet_by_index(0) #get the current sheet 
billing_vn = billing_sheet.col_values(1, start_rowx=0, end_rowx=None)#list of vouchers in billing reports 
for vn in station_vn: #For every voucher in station report 
    if vn: #if there is data 
     vnb=vn[1:] #change data 
     vnb=float(vnb) #change data type to float 
     if vnb in billing_vn: # check if voucher exist in billing report 
      row=station_vn.index(vn)+1 #take the row of current voucher 
      station_vn_data = station_sheet.row_values(row, start_colx=0, end_colx=15) #take the data for current row from station report 
      billing_vn_data = billing_sheet.row_values(billing_vn.index(vnb),start_colx=0, end_colx=15) #take the data for current voucher from billing report 
      if float(station_vn_data[5])==billing_vn_data[1]: #check if vouchers are equal 
       print "nomer na vouchera", station_vn_data[5], billing_vn_data[1] 
       if round(station_vn_data[10],3)<>round(billing_vn_data[5],3): #check for differences in ammount 
        print "Razlika v edinichna cena", round(station_vn_data[10],3),"-" , round(billing_vn_data[5],3),"=", round(station_vn_data[10]-billing_vn_data[5],3) 
       if station_vn_data[11]<>billing_vn_data[4]: #check for difference in price 
        print "kolichestvo", round(station_vn_data[11],4),"-", round(billing_vn_data[4],4),"=",round(station_vn_data[11]-billing_vn_data[4],4) #Ako ima razliki kolichestvata se printirat 
       if station_vn_data[12]<>billing_vn_data[6]:# check for 1 more difference 
        print "obshta suma", round(station_vn_data[12],3),"-", round(billing_vn_data[6],3),"=",round(station_vn_data[12]-billing_vn_data[6],3) 
       else: 
        print "voucher is OK" 
      print " " #print empty row for more clear view 
     else: #if voucher do not exist in billing 
      if vnb: 
       print vnb, "does not exist in billing report" #print the voucher number wich don`t exist 
+0

"여기 내 코드입니다, 지금 수정 그것 "정확히 여기에 도움을 얻는 방법이 아닙니다 ... [투어] (http://stackoverflow.com/tour) StackOverflow 작동 방식을 이해해 주시기 바랍니다 –

+1

'station_rep.sheet_by_index (0) # 마지막 시트 가져와야합니다. '- 무슨 근거로? '0'은 * first * 인덱스이고, -1은 마지막 인덱스입니다. – jonrsharpe

답변

0

: 엄마 코드가

for id in station_rep.sheet_names(): 
sheet_id=station_rep.sheet_names().index(id) 
station_sheet = station_rep.sheet_by_index(sheet_id) #get the last sheet 
+0

왜 station_rep.sheet_names()의 이름 대신'station_sheet = station_rep.sheet_by_name (name)'이 될까요? 또는'station_sheet for station_rep.sheets() :'에 대해? 마지막으로 원하는 것이면 ** 인덱스'-1' **을 사용하십시오. – jonrsharpe

+0

방법 : station_sheet = station_rep.sheet_by_index (-1) 잘 작동하고 가장 우아한 방법입니다. 도와 주셔서 감사합니다. –

2
station_sheet = station_rep.sheet_by_index(0) #should get the last sheet 

이 마지막 시트를 받아야 할 이유가 없다; 파이썬 지수는 0을 기준으로하므로 0 시퀀스의 요소이다 : 당신이 마지막 워크 시트이, 파이썬은 시퀀스의 끝에서 음의 색인을 수 있다는 점에 유의하십시오

>>> [1, 2, 3][0] 
1 

:

그 근거
>>> [1, 2, 3][-1] 
3 

, 나는 당신이 원하는 생각 :

station_sheet = station_rep.sheet_by_index(-1) # get the last sheet 
             #^note index 
관련 문제