2012-10-04 47 views
0

我有一些代碼可以通過PyWorkbooks很好的與excel spredsheets一起工作,但我想在熊貓上做同樣的工作。這是原代碼:熊貓替代字符串創建循環

from PyWorkbooks.ExWorkbook import ExWorkbook 
B = ExWorkbook() 
B.change_workbook("Somelist.xlsx") 
B.change_sheet("Tab1") 

#column 6 = 1 if row customer likes cucumber, 0 otherwise 
#column 7 = 1 if row customer likes carrot, 0 otherwise 
#column 8 = 1 if row customer likes spinach, 0 otherwise 
string1 = "Likes %s." 
string2 = "Likes %s and %s." 
string3 = "Likes %s, %s, and %s." 

def findveg(row): 
    veggies = [] 
    if B[row,6] == 1: 
     veggies.append('cucumber') 
    if B[row,7] == 1: 
     veggies.append('carrot') 
    if B[row,8] == 1: 
     veggies.append('spinach') 
    return tuple(veggies) 

for i in range(1,100): 
    if len(veggies) == 1: 
     veggies= findveg(i) 
     B[i,9] = string1 % veggies 
    if len(veggies) == 2: 
     veggies= findveg(i) 
     B[i,9] = string2 % veggies 
    if len(veggies) == 3: 
     veggies= findveg(i) 
     B[i,9] = string3 % veggies 
+0

你能解釋一下你想達到什麼樣的? – root

+0

@root,我自己解決了,希望我的目標明確如下。我需要根據一個人喜歡的素食來爲每一行做出正確的詞組。 ; 7) – dmvianna

回答

1
string1 = "Likes %s." 
string2 = "Likes %s and %s." 
string3 = "Likes %s, %s, and %s." 

def findveg(row): 
    veggies = [] 
    if DF['cucumber_lover'][row] == 1: 
     veggies.append('cucumber') 
    if DF['carrot_lover'][row] == 1: 
     veggies.append('carrot') 
    if DF['spinach_lover'][row] == 1: 
     veggies.append('spinach') 
    return tuple(veggies) 

flist['msg'] = '' 
for row in DF.index: 
     veggies = findfac(row) 
     if len(veggies) == 1: 
      findveg['msg'][row] = string1 % veggies 
     if len(veggies) == 2: 
      findveg['msg'][row] = string2 % veggies 
     if len(veggies) == 3: 
      findveg['msg'][row] = string3 % veggies