2014-09-02 70 views
-2

我有分裂包含了像etries將字符串分割:蟒蛇重新使用和查找到一個列表

80% Combed Cotton 20% Polyester Brushed 3-ply 320 g/m2 
70% Bamboo Viscose 30% Organic Cotton Fine Jersey 115g/3.4 oz 

,我喜歡將其分割爲:

('80% Combed Cotton', '20% Polyester', 'Brushed 3-ply', '320 g/m2') 
('70% Bamboo Viscose', '30% Organic Cotton', 'Fine Jersey', '115g/3.4 oz') 

...

我有織物和織物類型的查找表,如:

fabric = ['Combed Cotton', 'Polyester', 'Organic Cotton', 'Bamboo Viscose'] 
fabric_type = ['Brushed 3-ply', 'Fine Jersey'] 

任何意見非常讚賞

+0

條目的變量是多少?我懷疑每行可以有多個結構,只有一個fabric_type? – 2014-09-02 10:47:52

+0

這裏是列表https://gist.github.com/nkhine/fa51bb6cb6f958efd80f – khinester 2014-09-02 10:55:37

回答

0

有了一個表達:

import re 
fabric = ['Combed Cotton', 'Polyester', 'Organic Cotton', 'Certified Organic Cotton', 'Bamboo Viscose'] 
fabric_type = ['Brushed 3-ply', 'Fine Jersey'] 

test_cases = [ 
    "80% Combed Cotton 20% Polyester Brushed 3-ply 320 g/m2", 
    "70% Bamboo Viscose 30% Organic Cotton Fine Jersey 115g/3.4 oz"] 

fabric_rx = re.sub("\s+","\s+","|".join(fabric)) 
fabric_type_rx = re.sub("\s+","\s+","|".join(fabric_type)) 

expr = "(?:\d{1,3}\%\s(?:"+ fabric_rx +")(?:\s+))|(?:"+ fabric_type_rx +"(?:\s+))|(?:.{2,})" 
rx = re.compile(expr) 

for test in test_cases: 
    results = re.findall(rx, test) 
    for idx, item in enumerate(results): 
     results[idx] = item.strip() 
    print results 

但是,這些單片的正則表達式是慢...與狀態機大概標記化一個更好的主意......

UPDATE/w您的清單:

import re 
fabric = ['Combed Cotton', 'Polyester', 'Organic Cotton', 'Bamboo Viscose'] 
fabric_type = ['Brushed 3-ply', 'Fine Jersey'] 

test_cases = [ 
    "100% Certified Organic Cotton Jersey 155 g/4.6 oz.", 
    "100% Certified Organic Cotton Jersey 145 g/4.3 oz.", 
    "100% Certified Organic Cotton Jersey 155 g/4.6 oz.", 
    "100% Certified Organic Cotton Jersey 155 g/4.6 oz.", 
    "100% Certified Organic Cotton Jersey 155 g/4.6 oz.", 
    "100% Certified Organic Cotton Pique 180 g/5.3 oz.", 
    "100% Certified Organic Cotton Pique 180 g/5.3 oz.", 
    "100% Certified Organic Cotton Jersey 155 g/4.6 oz.", 
    "100% Certified Organic Cotton Jersey 155 g/4.6 oz.", 
    "100% Certified Organic Cotton Fine Jersey 115g/3.4 oz.", 
    "100% Certified Organic Cotton Fine Jersey 115g/3.4 oz.", 
    "100% Organic Cotton Brushed 3-ply 310 g/9.1 oz.", 
    "100% Organic Cotton Brushed 3-ply 310 g/9.1 oz.", 
    "100% Organic Cotton Brushed 3-ply 290 g/8.6 oz.", 
    "100% Organic Cotton Brushed 3-ply 290 g/8.6 oz.", 
    "100% Certified Organic Cotton Brushed 3-ply 280-300g/8.3-8.9 oz.", 
    "100% Certified Organic Cotton Brushed 3-ply 280-300g/8.3-8.9 oz.", 
    "100% Certified Organic Cotton Brushed 3-ply 280-300g/8.3-8.9 oz.", 
    "100% Certified Organic Cotton Brushed 3-ply 280-300g/8.3-8.9 oz.", 
    "100% Certified Organic Cotton Brushed 3-ply 280-300g/8.3-8.9 oz.", 
    "100% Organic Cotton Plain Weave 120 g/3.5 oz.", 
    "100% Organic Cotton Twill Weave 170 g/5.1 oz.", 
    "100% Certified Organic Cotton Jersey 155 g/4.6 oz.", 
    "100% Combed Cotton Interlock 240g/7.2 oz.", 
    "100% Combed Cotton Interlock 200g/6 oz.", 
    "100% Combed Cotton Jersey 165g/4.95 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Interlock 240g/7.2 oz.", 
    "100% Combed Cotton 1x1 Rib 240g/7.2 oz.", 
    "100% Combed Cotton Jersey 150g/4.5 oz", 
    "100% Combed Cotton Jersey 155g/4.65 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 85% Cotton 15% Viscose)", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 85% Cotton 15% Viscose)", 
    "100% Combed Cotton Jersey 150g/4.5 oz (Melange Grey: 74% Cotton 26% Polyester)", 
    "100% Combed Cotton Jersey 160g/4.8 oz.", 
    "100% Combed Cotton Sheer Jersey 90g/2.65 oz.", 
    "100% Combed Cotton Sheer Jersey 115g/3.4 oz.", 
    "70% Bamboo Viscose 30% Organic Cotton Fine Jersey 115g/3.4 oz.", 
    "70% Bamboo Viscose 30% Organic Cotton Fine Jersey 115g/3.4 oz.", 
    "70% Bamboo Viscose 30% Organic Cotton Jersey 150g/4.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "45% Combed Cotton 45% Modal 10% Lycra Elastane Jersey 240g/7.2 oz.", 
    "100% Organic Cotton Canvas Weave 340g", 
    "100% Tencel Lyocell Fine Jersey 120g/3.5 oz.", 
    "100% Tencel Lyocell Fine Jersey 120g/3.5 oz.", 
    "100% Tencel Lyocell Fine Jersey 120g/3.5 oz.", 
    "100% Combed Cotton Fine Jersey 115g", 
    "100% Certified Organic Cotton Brushed 3-ply 280/8.3 oz.", 
    "100% Certified Organic Cotton Interlock 200/5.9 oz.", 
    "100% Certified Organic Cotton Interlock 200/5.9 oz.", 
    "100% Certified Organic Cotton Interlock 200/5.9 oz.", 
    "100% Certified Organic Cotton Interlock 200/5.9 oz.", 
    "100% Organic Cotton Brushed 3-ply 290 g/8.6 oz.", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "80% Combed Cotton 20% Polyester Brushed 3-Ply 320g/9.4 oz", 
    "80% Combed Cotton 20% Polyester Brushed 3-Ply 320g/9.4 oz", 
    "100% Tencel Lyocell Fine Jersey 120g/3.5 oz", 
    "100% Tencel Lyocell Fine Jersey 120g/3.5 oz", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "Printed on FSC Mix certified paper using vegetable inks", 
    "96% Cotton 4% Polyester Fine Speckled Jersey 130-150 g/m2", 
    "96% Cotton 4% Polyester Fine Speckled Jersey 130-150 g/m2", 
    "100% Combed Cotton Supersoft Jersey 175 g/m2", 
    "60% Combed Cotton 40% Polyester Brushed 3-ply 280 g/m2", 
    "60% Combed Cotton 40% Polyester Brushed 3-ply 280 g/m2", 
    "100% Combed Cotton Supersoft Jersey 175 g/m2", 
    "100% Combed Cotton Supersoft Jersey 175 g/m2", 
    "100% Combed Cotton Jersey 135 g/m2 (Mélange Grey: 60% Cotton 40% Polyester) Jersey 135 g/m2", 
    "70% Bamboo Viscose 30% Organic Cotton Jersey 150 g/m2", 
    "70% Bamboo Viscose 30% Organic Cotton Jersey 115 g/m2", 
    "100% Organic Cotton Jersey 155 g/m2", 
    "100% Organic Cotton Jersey 155 g/m2", 
    "60% Recycled Organic Cotton 40% Recycled Polyester Jersey 165g/4.95 oz.", 
    "60% Recycled Organic Cotton 40% Recycled Polyester Jersey 165g/4.95 oz.", 
    "60% Recycled Organic Cotton 40% Recycled Polyester Brushed 2-ply 280/8.3 oz.", 
    "60% Recycled Organic Cotton 40% Recycled Polyester Twill Weave 220 g/6.5 oz.", 
    "60% Recycled Organic Cotton 40% Recycled Polyester Twill Weave 220 g/6.5 oz.", 
    "100% Combed Cotton Interlock 240g/7.2 oz.", 
    "100% Combed Cotton Jersey 165g/4.95 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Sheer Jersey 115g/3.4 oz.", 
    "100% Combed Cotton Jersey 150g/4.5 oz", 
    "100% Combed Cotton Jersey 155g/4.65 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Jersey 150 g/m2 (Mélange Grey: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "50% Cotton, 50% Polyester Jersey 160g/4.7 oz.", 
    "50% Cotton, 50% Polyester Jersey 160g/4.7 oz.", 
    "100% Combed Cotton Sheer Jersey 90g/2.65 oz.", 
    "100% Combed Cotton Sheer Jersey 90g/2.65 oz.", 
    "100% Combed Cotton Sheer Jersey 105g/3.15 oz.", 
    "100% Combed Cotton Sheer Jersey 115g/3.4 oz.", 
    "70% Bamboo Viscose 30% Organic Cotton Fine Jersey 115g/3.4 oz.", 
    "70% Bamboo Viscose 30% Organic Cotton Jersey 150g/4.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester Brushed 3-ply 320 g/m2", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester Brushed 3-ply 320 g/9.4 oz", 
    "80% Combed Cotton 20% Polyester 320g/9.4 oz.", 
    "80% Combed Cotton 20% Polyester Brushed 3-Ply 320g/9.4 oz", 
    "80% Combed Cotton 20% Polyester Brushed 3-Ply 320g/9.4 oz", 
    "100% Tencel Lyocell Fine Jersey 120g/3.5 oz.", 
    "100% Combed Cotton Fine Jersey 115g/3.4 oz. (Melange Grey: 60% Cotton 40% Polyester)", 
    "100% Certified Organic Cotton Interlock 200 g/m2", 
    "100% Certified Organic Cotton Interlock 200 g/m2", 
    "100% Certified Organic Cotton Interlock 200 g/m2", 
    "100% Certified Organic Cotton Interlock 200 g/m2", 
    "100% Combed Cotton Jersey 150g/4.5 oz (Melange Grey: 74% Cotton 26% Polyester)", 
    "100% Combed Cotton Jersey 150g/4.5 oz", 
    "100% Combed Cotton Jersey 130g/3.8 oz", 
    "100% Combed Cotton Jersey 130g (Mélange Grey: 85% Cotton 15% Viscose)", 
    "100% Combed Cotton Jersey 150g (Mélange Grey: 85% Cotton 15% Viscose) (Other Mélange: 60% Cotton 40% Polyester)", 
    "100% Combed Cotton Jersey 150g/4.5 oz", 
    "100% Combed Cotton Jersey 150g/4.5 oz", 
    "            | 99% Cotton 1% Viscose)", 
    "100% Combed Cotton Jersey 130g", 
    " |0% Combed Cotton Fine Jersey 115g (Mélange Grey: 85% Cotton 15% Viscose) (Mélange White: 99% Cotton 1% Viscose)", 
    "80% Combed Cotton 20% Polyester Brushed 3-Ply 320g/9.4 oz", 
    "100% Combed Organic Cotton Jersey 155g/4.6 oz", 
    "100% Combed Cotton Jersey 130g" 
] 

fabric_rx = re.sub("\s+","\s+","|".join(fabric)) 
fabric_type_rx = re.sub("\s+","\s+","|".join(fabric_type)) 

expr = "(?:\d{1,3}\%\s(?:"+ fabric_rx +")(?:\s+))|(?:"+ fabric_type_rx +"(?:\s+))|(?:.{2,})" 
rx = re.compile(expr) 

for test in test_cases: 
    try: 
     results = re.findall(rx, test) 
     for idx, item in enumerate(results): 
      results[idx] = item.strip() 
     print results 
    except: 
     print "Format error: %s" % test 

但是,您需要使用列表中的材料擴展結構列表...其實我是爲您做的,因爲只有'Certified Organic Cotton'從列表中缺失。

0

沒有指定分隔符,只有關鍵字,所以找到每個關鍵字百分比,在結構的情況下,或在fabric_type情況下的前導字符串。
試試這個

>>> lines = ['80% Combed Cotton 20% Polyester Brushed 3-ply 320 g/m2', 
... '70% Bamboo Viscose 30% Organic Cotton Fine Jersey 115g/3.4 oz'] 
>>> fabrics = ['Combed Cotton', 'Polyester', 'Organic Cotton', 'Bamboo Viscose'] 
>>> fabric_types = ['Brushed 3-ply', 'Fine Jersey'] 
>>> import re 
>>> result = [] 
>>> for line in lines: 
...  product = [] 
...  for fabric in fabrics: 
...   for match in re.findall('[\d\s%%]*\s%s'%fabric, line): 
...    product.append(match.strip()) 
...  for fabric_type in fabric_types: 
...   for match in re.findall('%s.+'%fabric_type, line): 
...    product.append(fabric_type) 
...    product.append(match.replace(fabric_type,"").strip()) 
...  if res: 
...   result.append(product) 
... 
>>> result 
[['80% Combed Cotton', '20% Polyester', 'Brushed 3-ply', '320 g/m2'], ['30% Organic Cotton', '70% Bamboo Viscose', 'Fine Jersey', '115g/3.4 oz']]