3.2.5.2 simulate the C function scanf () function, 3.2.5.2scanf
In Python, there is no function directly equivalent to scanf (). Therefore, to format the input, you need to use the regular expression function. In addition, the function of the regular expression is better than that of scanf () more flexible and more powerful functions. The following describes some Equivalent Expressions:
Scanf () Format String |
Regular Expression |
% C |
. |
% 5c |
. {5} |
% D |
[-+]? \ D + |
% E, % E, % f, % g |
[-+]? (\ D + (\. d *)? | \. \ D +) ([eE] [-+]? \ D + )? |
% I |
[-+]? (0 [xX] [\ dA-Fa-f] + | 0 [0-7] * | \ d +) |
% O |
[-+]? [0-7] + |
% S |
\ S + |
% U |
\ D + |
% X, % X |
[-+]? (0 [xX])? [\ DA-Fa-f] + |
Example of a string:
/Usr/sbin/sendmail-0 errors, 4 warnings
For strings in the preceding format, if you use the C function scanf () for input, you must use the following format:
% S-% d errors, % d warnings
If we use a regular expression, it is represented as follows:
(\ S +)-(\ d +) errors, (\ d +) warnings
Example:
Print ('scanf ()')
Pattern = re. compile (r "(\ S +)-(\ d +) errors, (\ d +) warnings ")
Match = pattern. match ('/usr/sbin/sendmail-0 errors, 4 warnings ')
If match:
Print (match. groups ())
The output is as follows:
Scanf ()
('/Usr/sbin/sendmail', '0', '4 ')
Example of % c:
Print ('scanf () % C ')
Pattern = re. compile (r ".")
Match = pattern. match ('this is for test \ n ')
If match:
Print (match. group ())
The output is as follows:
Scanf () % c
T
Example of % 5c:
Print ('scanf () % 5c ')
Pattern = re. compile (r ". {5 }")
Match = pattern. match ('this is for test \ n ')
If match:
Print (match. group ())
The output is as follows:
Scanf () % 5c
This
Example of % e, % E, % f, % g:
Print ('scanf () % e, % E, % f, % G ')
Pattern = re. compile (r "[-+]? (\ D + (\. \ d *)? | \. \ D +) ([eE] [-+]? \ D + )? ")
Match = pattern. match ('+ 200.3721 \ n ')
If match:
Print (match. group ())
Match = pattern. match ('x9876 \ n ')
If match:
Print (match. group () # unmatched no output
The output is as follows:
Scanf () % e, % E, % f, % g
+ 200.3721
Example of % I:
Print ('scanf () % I ')
Pattern = re. compile (r "[-+]? (0 [xX] [\ dA-Fa-f] + | 0 [0-7] * | \ d + )")
Match = pattern. match ('0xaa55 \ n ')
If match:
Print (match. group ())
Match = pattern. match ('192. 56 \ n ')
If match:
Print (match. group ())
The output is as follows:
Scanf () % I
0xAA55
234
Example of % o of octal:
Print ('scanf () % o ')
Pattern = re. compile (r "[-+]? [0-7] + ")
Match = pattern. match ('2014 \ n ')
If match:
Print (match. group ())
Match = pattern. match ('2014 \ n ')
If match:
Print (match. group () # unmatched no output
The output is as follows:
Scanf () % o
0756
Example of string % s:
Print ('scanf () % s ')
Pattern = re. compile (r "\ S + ")
Match = pattern. match ('shenzhen is a fishing village \ n ')
If match:
Print (match. group ())
Match = pattern. match ('2014 \ n ')
If match:
Print (match. group ())
The output is as follows:
Scanf () % s
Shenzhen is a small fishing village
898
Example of % u:
Print ('scanf () % U ')
Pattern = re. compile (r "\ d + ")
Match = pattern. match ('2014 \ n ')
If match:
Print (match. group ())
Match = pattern. match ('-898 \ n ')
If match:
Print (match. group () # unmatched no output
The output is as follows:
Scanf () % u
756
Example of hexadecimal % x, % X:
Print ('scanf () % x % x ')
Pattern = re. compile (r "[-+]? (0 [xX]) [\ dA-Fa-f] + ")
Match = pattern. match ('0x756 \ n ')
If match:
Print (match. group ())
Match = pattern. match ('-898 \ n ')
If match:
Print (match. group () # unmatched no output
The output is as follows:
Scanf () % x % X
Zero x 756
Cai junsheng QQ: 9073204 Shenzhen
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.