python使用re和查找列表进行拆分

时间:2014-09-02 10:39:39

标签: python

我必须分割一个包含如下符号的字符串:

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']

任何建议非常感谢

2 个答案:

答案 0 :(得分:0)

没有指定分隔符,只指定了关键字,因此在Fabric格式中找到每个关键字的百分比,或者在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']]

答案 1 :(得分: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

但是这些整体的正则表达式很慢......用状态机进行标记化可能是一个更好的想法......

更新/列表:

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

但是您需要使用列表中的材料扩展结构列表......实际上我是为您做的,因为列表中缺少“有机认证棉花”。