2014-10-03 4 views
-1

지저분한 인벤토리 목록 (약 10K)을 정리해야하고이를 달성하기 위해 파이썬에서 정규식을 사용하는 데 몇 가지 문제가 있습니다.파이썬에서 지저분한 지저분한 문자열

product_pool=["#101 BUMP STOPPER RAZOR BUMP TREATMENT SENSITIVE SKIN FORMULA", 
       "#W65066CS - Cell phone, Triangle wand & 5 sections lip gloss", 
       "(Archived)S.O.S. Steel Wool Soap Pads", 
       "(ARCHIVED) HTH Spa pH Increaser", 
       "****GLUE STICKS", 
       "-20°F Splash Windshield Washer Fluid", 
       "01127 – Fing’rs Mighty Drop, 3g", 
       "10-01130-Brush On Nail Glue (Three Bond TB1743)", 
       "Aveeno® Continuous Protection Sunblock Spray Products"] 

적으로는, 내가이, #, *, ®, –, °F 같은 상징을 제거하기 위해 괄호 (Archived), (Three Bond TB1743)에서 101, 10-01130-, 01127 같은 숫자 및 세계를 좋아하는 것 : 여기 내 목록의 작은 샘플입니다.

product_pool=["BUMP STOPPER RAZOR BUMP TREATMENT SENSITIVE SKIN FORMULA", 
       "Cell phone, Triangle wand 5 sections lip gloss", 
       "S.O.S. Steel Wool Soap Pads", 
       "HTH Spa pH Increaser", 
       "GLUE STICKS", 
       "Splash Windshield Washer Fluid", 
       "Fing'rs Mighty Drop", 
       "Brush On Nail Glue", 
       "Aveeno Continuous Protection Sunblock Spray Products"] 

내 접근 방식은 내가 유지하고 모든 문자를 유지하지 않으려는 기호로 제품을 분할처럼 그리고 최종 출력이 보일 것이다. 그러나이 방법은 효과가없는 것 같습니다. 그래서 나는 어떤 제안을 주셔서 감사합니다!

BUMP STOPPER RAZOR BUMP TREATMENT SENSITIVE SKIN FORMULA 
Cell phone Triangle wand sections lip gloss 
Steel Wool Soap Pads (S.O.S. is missing) 
HTH Spa pH Increaser 
GLUE STICKS 
Splash Windshield Washer Fluid 
Mighty Drop (Fing'rs is missing) 
Brush On Nail Glue Bond 
Continuous Protection Sunblock Spray Products (Aveeno is missing) 

답변

3
product_pool=["#101 BUMP STOPPER RAZOR BUMP TREATMENT SENSITIVE SKIN FORMULA", 
       "#W65066CS - Cell phone, Triangle wand & 5 sections lip gloss", 
       "(Archived)S.O.S. Steel Wool Soap Pads", 
       "(ARCHIVED) HTH Spa pH Increaser", 
       "****GLUE STICKS", 
       "-20°F Splash Windshield Washer Fluid", 
       "01127 – Fing’rs Mighty Drop, 3g", 
       "10-01130-Brush On Nail Glue (Three Bond TB1743)", 
       "Aveeno® Continuous Protection Sunblock Spray Products"] 

아직 몇 가지 여분의 공간을, 그러나 이것은 대략가는 하나 개의 방법이 될 수있다 :

for product in product_pool: 
    product_split=re.split(' |, |[) |* |-]', product) 
    print ' '.join(ch for ch in product_split if ch.isalpha()) 

그리고 출력 본다.

import string 
goodChars = string.ascii_letters + '.' + ' ' 
cleaned = [''.join(i for i in word if i in goodChars) for word in product_pool] 

>>> cleaned 
[' BUMP STOPPER RAZOR BUMP TREATMENT SENSITIVE SKIN FORMULA', 
'WCS Cell phone Triangle wand sections lip gloss', 
'ArchivedS.O.S. Steel Wool Soap Pads', 
'ARCHIVED HTH Spa pH Increaser', 
'GLUE STICKS', 
'F Splash Windshield Washer Fluid', 
' Fingrs Mighty Drop g', 
'Brush On Nail Glue Three Bond TB', 
'Aveeno Continuous Protection Sunblock Spray Products'] 

당신은 당신이 계속 string.punctuation, string.ascii_letters 같은 것들에 대한 string constants을 확인하고 싶은 문자로 주위를 재생할 수있는 등

+0

의견을 보내 주셔서 감사합니다. 이 정리 작업에는 여러 단계가 필요합니다. 'string.ascii_letters + '를 확인합니다.' + ''' –

1

당신은 re.subregex 대체를 사용할 수 있습니다.

import re 

pattern = '[^a-zA-Z\s]|(?i)archived' 
results = [re.sub(pattern, '', s).strip() for s in product_pool] 
# ['BUMP STOPPER RAZOR BUMP TREATMENT SENSITIVE SKIN FORMULA', 
# 'WCS Cell phone Triangle wand sections lip gloss', 
# 'SOS Steel Wool Soap Pads', 
# 'HTH Spa pH Increaser', 
# 'GLUE STICKS', 
# 'F Splash Windshield Washer Fluid', 
# 'Fingrs Mighty Drop g', 
# 'Brush On Nail Glue Three Bond TB', 
# 'Aveeno Continuous Protection Sunblock Spray Products'] 

...에없는 모든 항목과 일치 [^...] 정규식 패턴. 그런 다음 re.sub을 사용하여 이러한 모든 일치 항목을 빈 문자열로 대체하여 효과적으로 삭제할 수 있습니다. 패턴의 두 번째 항목은 청크와 일치하고 (?i)은 해당 항목의 대소 문자를 무시하도록 지시합니다.

관련 문제