Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the function re.sub bytes string which is easy to be encountered in python data cleaning

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report