Thursday 15 April 2010

python - How to remove the punctuation in the middle of a word or numbers? -


For example, if I have a string of numbers and a list of words:
  My_number = ("5,6! 7,8") My_word = ["Hail? Llo", "intro"]    

str.translate :

  & gt; & Gt; & Gt; String import with punctuation marks & gt; & Gt; & Gt; Lis = ["hel? Llo", "intro"]> gt; & Gt; & Gt; [Translate for X (none, punctuation marks) in LIS X) ['Halolo', 'Introduction']> gt; & Gt; & Gt; Strs = "5,6! 7,8" & gt; & Gt; & Gt; Using strakeslate (none, punctuation marks) '5678'   

regex :

  & gt; & Gt; & Gt; Import re & gt; & Gt; & Gt; [Again: .sub (r '[{}] +'. Format (Punctuation), '', X) X in LIS ['Hello', 'Introduction']> gt; & Gt; & Gt; Re.sub (r '[{}] +'. Format (Punctuation), '', strs) '5678'   

Understand the list and str.join :

  & gt; & Gt; & Gt; ["" ". [(Halo ',' Introduction '] >> gt; & gt; [' X to C for X in C for C]". ((For C, CR for C C) No '5678'   

Function:

>  Import from defaults (my_strip) Args): ifinstance (args, not fitable) and isinstance (args, basestring) No: return [x.translate (none, punctuation marks) in args for x] else: return args.translate (none, punctuation marks ) ... 
My_strip ("5,6! 7,8") '5678'>> My_strip (["Hail? Llo", "intro"] ) ['Hello', 'intro']

No comments:

Post a Comment