2017-04-16 16 views
-1

所以我已經有了這種在ftp上查找特定jpgs的非常糟糕的方法。 它找到我有一個excel文件並檢查ftp。 你們會如何讓這個更正確的更快/更容易/更普遍?替代在FTP服務器上查找文件?

ftp = FTP("ftp.helmethouse.com") 
g=ftp.login(user ='username',passwd = 'password') 

wb = openpyxl.load_workbook(r'C:\Users\aaron\Documents\HustleHardRacing\Shoei HJC Colors only.xlsx') 
sheet = wb.get_sheet_by_name('Shoei HJC full colors and sizes') 

def check_line_for_img(line): 
    for i in range(2,143): 
     x = sheet.cell(row =i, column = 3).value 
     k = ['r.jpg', 'top.jpg', 't.jpg', 'rer.jpg', 'rt.jpg'] 
     line = line[56:] 
     if x is None: 
      pass 
     else: 
      for i in range(len(k)): 
       y = (x[:-2]+'03'+k[i]) 
       print(y) 
       if line == y: 
        print(line) 
       elif y is None: 
        print ('Error!') 
        pass 
       else: 
        pass 
k = ftp.retrlines('LIST', check_line) 
+1

定義 「更普遍的」 –

+2

注:如果這個代碼不以某種方式破解,請嘗試在此處詢問https://codereview.stackexchange.com/tour –

+0

我會問「一般來說,搜索特定值的項目列表的最佳做法是什麼。」但我可能只是去找我自己 –

回答

0

則可以使用set()做,比如:

from openpyxl.workbook.workbook import Workbook as _Workbook 

def normalize(l, has_ext=False): 
    for i in range(len(l)): 
     if has_ext: l[i] = l[i][:-4] 
     l[i] = l[i][:-2] 
    return l 

wb = _Workbook() 
ws = wb.worksheets[0] 
data = [['', '', 'Images'], ['', '', 'r01'], ['', '', 'top01'], ['', '', 't01'], ['', '', 'rer01'], ['', '', 'rt01']] 
for row, rData in enumerate(data, 1): ws.append(rData) 

x = [c[0].value for c in ws['C2:C6']] 
print('x=%s' % str(x)) 

k = ['r03.jpg', 'top03.jpg', 't03.jpg', 'rer03.jpg', 'rt03.jpg', 'rr03.jpg'] 

x_set = set(normalize(x)) 
k_set = set(normalize(k, True)) 
print('x_set=%s\nk_set=%s' % (x_set, k_set)) 

x_diff = k_set.difference(x_set) 
if x_diff: 
    print('x_set missing:%s' % (x_diff)) 

k_diff = x_set.difference(k_set) 
if k_diff: 
    print('k_set missing:%s' % (k_diff)) 
else: 
    print('k_set missing None') 

輸出
X = [ 'R01', 'TOP01', 'T01', 'rer01' ,'rt01']
x_set = {'top','r','rer','t','rt'}
k_set = {'top','rer','t','r' ,'rt','rr'}
x_set missing: { 'RR'}
k_set缺少無

測試與Python 3.4.2 - openpyxl:2.4.1