MCPcopy Create free account
hub / github.com/ActiveState/code / scanf

Function scanf

recipes/Python/502213_Simple_scanfimplementation/recipe-502213.py:97–130  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

95
96
97def 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

Callers

nothing calls this directly

Calls 4

_scanf_compileFunction · 0.85
rangeFunction · 0.85
readlineMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected