In Python , there is no function function that is directly equivalent to scanf (), so you need to format the input, you need to use the function of regular expression, and the function of regular expression is more than scanf() more flexible and more powerful, here are some of the equivalent expressions:
scanf () format string |
Regular expressions |
%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]+ |
Enter an example of a string:
/usr/sbin/sendmail-0 errors, 4 warnings
For strings in the format above, if you use the C function scanf() to enter, you need to use the following format to implement:
%s-%d errors,%d warnings
If we use a regular expression to represent it 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 resulting output is as follows:
scanf ()
('/usr/sbin/sendmail ', ' 0 ', ' 4 ')
Example of%c:
Print (' scanf ()%c ')
Pattern = Re.compile (r ".")
Match = Pattern.match (' This was for test\n ')
If match:
Print (Match.group ())
The resulting output is as follows:
scanf ()%c
T
examples of%5c:
Print (' scanf ()%5c ')
Pattern = Re.compile (r ". { 5} ")
Match = Pattern.match (' This was for test\n ')
If match:
Print (Match.group ())
The resulting output is as follows:
scanf ()%5c
This
%e,%e,%f,%g Example:
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 ()) # mismatch no output
The resulting output is as follows:
scanf ()%e,%e,%f,%g
+200.3721
examples 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 (' 234.56\n ')
If match:
Print (Match.group ())
The resulting output is as follows:
scanf ()%i
0xaa55
234
An example of an octal %o :
Print (' scanf ()%o ')
Pattern = Re.compile (r "[-+]?[ 0-7]+ ")
Match = Pattern.match (' 0756\n ')
If match:
Print (Match.group ())
Match = Pattern.match (' 898\n ')
If match:
Print (Match.group ()) # mismatch no output
The resulting 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 small fishing village \ n ')
If match:
Print (Match.group ())
Match = Pattern.match (' 898\n ')
If match:
Print (Match.group ())
The resulting 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 (' 756\n ')
If match:
Print (Match.group ())
Match = Pattern.match (' -898\n ')
If match:
Print (Match.group ()) # mismatch no output
The resulting output is as follows:
scanf ()%u
756
example of hex %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 ()) # mismatch no output
The resulting output is as follows:
scanf ()%x
0x756
Cai Junsheng qq:9073204 Shenzhen
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
3.2.5.2 analog C function scanf () function