scanf supports the following formats: %c One character %5c 5 characters %d int value %7d int value with length 7 %f float value %o octal value %X, %x hex value %s string terminated by whitespace Examp
(format, s=None)
| 95 | |
| 96 | |
| 97 | def scanf(format, s=None): |
| 98 | """ |
| 99 | scanf supports the following formats: |
| 100 | %c One character |
| 101 | %5c 5 characters |
| 102 | %d int value |
| 103 | %7d int value with length 7 |
| 104 | %f float value |
| 105 | %o octal value |
| 106 | %X, %x hex value |
| 107 | %s string terminated by whitespace |
| 108 | |
| 109 | Examples: |
| 110 | >>> scanf("%s - %d errors, %d warnings", "/usr/sbin/sendmail - 0 errors, 4 warnings") |
| 111 | ('/usr/sbin/sendmail', 0, 4) |
| 112 | >>> scanf("%o %x %d", "0123 0x123 123") |
| 113 | (66, 291, 123) |
| 114 | |
| 115 | |
| 116 | If the parameter s is a file-like object, s.readline is called. |
| 117 | If s is not specified, stdin is assumed. |
| 118 | |
| 119 | The function returns a tuple of found values |
| 120 | or None if the format does not match. |
| 121 | """ |
| 122 | |
| 123 | if s == None: s = sys.stdin |
| 124 | if hasattr(s, "readline"): s = s.readline() |
| 125 | |
| 126 | format_re, casts = _scanf_compile(format) |
| 127 | found = format_re.match(s) |
| 128 | if found: |
| 129 | groups = found.groups() |
| 130 | return tuple([casts[i](groups[i]) for i in range(len(groups))]) |
| 131 | |
| 132 | |
| 133 |
nothing calls this directly
no test coverage detected