An example Analysis of the function re.sub bytes string which is easy to be encountered in python data cleaning
The purpose of this article is to share with you the content of an example analysis of re.sub bytes string, a function that is easy to encounter in python data cleaning. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Re.sub
Function, a more powerful replacement function than replace, replacing the module on regular expression matching with repl
Re.sub (pattern, repl, string, count=0, flags=0)
Returns the string qualified by the leftmost regular expression that is replaced by repl. If the regular expression does not match, the string is not modified.
\ n is converted to a single newline character
\ r is converted to a carriage return, and so forth. Unknown escapes such as\ j are left alone. If it is followed by the number such as\ 6, replace the sixth set of strings, group 6 in the pattern. For example:
> re.sub (r'def\ s + ([a murzAmurz ZZ] [a-zA-Z_0-9] *)\ s *\ (\ s *\):',. R'static PyObject*\ npy_\ 1 (void)\ n {',... 'def myfunc (): 'static PyObject*\ npy_myfunc (void)\ n {'
If repl is a function, a call occurs for each pattern that does not overlap. This function takes a single matching object parameter and returns a replacement string. For example:
> def dashrepl (matchobj):... If matchobj.group (0) ='-': return''... Else: return'-'> > re.sub ('- {1 sAND 2}', dashrepl, 'pro----gram-files')' pro--gram files' > re.sub (r'\ sAND\ s,'&', 'Baked Beans And Spam', flags=re.IGNORECASE)' Baked Beans & Spam'
The template can be a string or a RE object
Count is the maximum number of replacements, which is a non-negative integer. If omitted or 0 is taken, the full file will be matched and replaced.
Class bytes ([source [, encoding [, errors])
Returns a new array object that cannot be modified to array elements. Value range of each element: 0