2011-10-10 3 views
1

나는 액세스 테이블에서 데이터를 가져와 엑셀 테이블에 쓰는 스크립트를 작성 중입니다. 이 과정에서 나는 excal 테이블을 포맷하려고합니다. 내 스크립트 아래에 Excel 테이블의 출력을 붙여 넣었습니다.값을 그룹화하여 합계를 작성하고 Excel로 작성하기, Python

다음 스크립트에서 데이터를 액세스 테이블에서 가져 와서 Excel 테이블로 가져옵니다. 첫 번째 열 (예 : 모든 M1, 모든 M20a 등)의 모든 값을 그룹화합니다. 두 번째 열은 관련 면적입니다. 세 번째 열은 각 그룹화 된 값의 면적 합계입니다.

모든 영역의 합계가 정확하며 마지막 그룹 (ZWA)을 제외하고 올바른 위치에 있습니다. 그 값의 합계를 쓰는 것은 아닙니다. 필자는 테이블의 마지막 행을 감지하도록 스크립트를 설정해야한다고 생각합니다.

import arcpy, xlwt, sys, traceback, datetime 
from arcpy import env 
from xlwt import * 
from itertools import groupby 
from collections import defaultdict 
from time import strftime 


# Set workspace for the file to be used 
env.workspace = "Z:\TestFolder" 
env.overwriteOutput = True 


# Stores access table into a variable and sorts the SMU field ascendingly 
cur = arcpy.SearchCursor("Access Table", "", "", "SMU", "SMU A") 

# Create excel workbook 
book = Workbook() 
sheet1 = book.add_sheet('Sheet 1') 

# Create a dictionary 
col_counts = defaultdict(int) 

# Set a varialb eto be used in looping through rwos and detecting when the value in the cell is different from the last 
last_value = object() 

# Set the start of 2 counters. rowx is to count rows in the access table. rowadd is to add the values in a field alled row.SHAPE_Area 
rowx = 3 
rowadd = 0 

# Loop through the access table 
for row in cur: 

    # Ask if the current value is not equal to the last value in the row.SMU column 
    if row.SMU != last_value: 

     last_value = row.SMU 

     # if the current value doesn't equal the last value, then place the sum of the row.SHAPE_Area field for the last value in a new cell in a different column. 
     sheet1.write(int(rowx+1),3,rowadd/10000) 

     # Reset counter to 0 
     rowadd = 0 
     # Add 2 to the counter to create a space between groups of values in the excel table 
     rowx += 2 
    else: 
     # Else only add 1 to the counter if the value in the cell reamisn the same as the last one 
     rowx += 1 


    # if the value of the row is the same as the last one, then add the values for a second column together and write to the excel sheet 
    if row.SMU == str(last_value): 

     rowadd += row.SHAPE_Area 
     print rowadd 

     sheet1.write(int(rowx),0,row.SMU) 
     sheet1.write(int(rowx),1,row.SHAPE_Area/10000) 

# Set the counter to += the last value in the (col_counts[last_value]) variable and start over again 
rowx += (col_counts[last_value]) 

# Save the workbook 
book.save("Z:\TestFolder\simple.xls") 

다음은 Excel의 출력 테이블입니다. 제 3 기둥에 ZWA 총 면적이 표시되지 않는지 확인하십시오. 누구나 이것이 왜 제안 할 수 있습니까? 세 번째 열의 첫 번째 행에서도 0을 제거하고 싶습니다.

     0 
M1  0.076492721  
M1  0.406600839  
M1  2.98016238  
         3.46325594 
M20a 0.665489193  
         0.665489193 
M21  0.005333282  
         0.005333282 
M23b 0.190245719  
M23b 0.233315779  
         0.423561498 
S1  0.201021287  
S1  0.176390376  
S1  0.200409435  
S1  0.009312814  
S1  0.071782163  
         0.658916076 
ZWA  0.387293182  

답변

0

아래와 같은 (테스트되지 않은) 코드가 필요합니다.

import arcpy, xlwt, sys, traceback, datetime 
from arcpy import env 
#### from xlwt import * 
#### (a) horrid, you need only 1 class (b) you already imported xlwt 
### from itertools import groupby 
#### good idea, but get some simple programming skills first 
#### from collections import defaultdict #### why? 
from time import strftime 

# Set workspace for the file to be used 
env.workspace = "Z:\TestFolder" 
env.overwriteOutput = True 

# Stores access table into a variable and sorts the SMU field ascendingly 
cur = arcpy.SearchCursor("Access Table", "", "", "SMU", "SMU A") 

# Create excel workbook 
book = Workbook() 
sheet1 = book.add_sheet('Sheet 1') 

# Create a dictionary #### what for? 
col_counts = defaultdict(int) 

# Set a varialb eto be used in looping through rwos 
# and detecting when the value in the cell is different from the last 
previous = dummy = object() 

# Set the start of 2 counters. 
# rowx is to count rows in the excel table, NOT the access table 
# rowadd is to add the values in a field alled row.SHAPE_Area 
rowx = 3 
rowadd = 0 
# Loop through the access table 
for row in cur: 
    if row.SMU != previous != dummy: 
     # if the current value doesn't equal the previous value, 
     # AND it's not the first row, then place 
     # the sum of the row.SHAPE_Area field for the previous value 
     # in a new cell in a different column. 
     rowx += 1 # start a new output row 
     sheet1.write(rowx, 3, rowadd/10000) 
     # Reset counter to 0 
     rowadd = 0 
    rowx += 1 # start a new output row 
    rowadd += row.SHAPE_Area 
    print rowadd 
    sheet1.write(rowx, 0, row.SMU) 
    sheet1.write(rowx, 1, row.SHAPE_Area/10000) 
    previous = row.SMU 

# End of input. Write the final subtotal (unless there was no input) 
if previous != dummy: 
    rowx += 1 
    sheet1.write(rowx, 3, rowadd/10000) 

##### What is the purpose of this???  
# Set the counter to += the last value in the 
# (col_counts[previous]) variable and start over again 
rowx += (col_counts[previous]) 

# Save the workbook 
book.save(r"Z:\TestFolder\simple.xls") 
관련 문제