简体   繁体   中英

Python function for taking formatted input from user similar to scanf() in 'C'

I was wondering if there is a function in Python to take formatted input from user similar to taking the input from user in 'C' using scanf() and format strings such as %d , %lf , etc.

Hypothetical example in which scanf() returns a list :

input_date_list = scanf("%d-%d-%d")
# User enters "1969-04-20"
input_time_list = scanf("%d:%d")
# User enters "04:20"
print(input_date_list, input_time_list)
# Output is "[1969, 4, 20] [4, 20]"

input() . For example this could be used as:

Name = input("Please enter your name?")

For use in Python 2, this would be raw_input() . For example this could be used as:

Name = raw_input("Please enter your name?")

What the Standard Library provides

There is no direct scanf(3) equivalent in the standard library. The documentation for the re module suggests itself as a replacement, providing this explanation :

Python does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf() format strings. The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions.

Modifying your hypothetical example, to work with regular expressions, we get...

import re

input_date_list = re.match(r"(?P<month>[-+]?\d+)-(?P<day>[-+]?\d+)-(?P<year>[-+]?\d+)", input())
print(f"[{input_date_list['year']}, {input_date_list['month']}, {input_date_list['day']}]")

input_time_list = re.match(r"(?P<hour>[-+]?\d+):(?P<minute>[-+]?\d+)", input())
print(f"[{input_time_list['hour']}, {input_time_list['minute']}]")

...and when executed:

python script.py << EOF
1-2-2023
11:59
EOF
[2023, 1, 2]
[11, 59]

Community Implementation

The scanf module on Pypi provides an interface much more akin to scanf(3).

This provides a direct implementation of your hypothetical examples:

from scanf import scanf

input_date_list = scanf("%d-%d-%d")
input_time_list = scanf("%d:%d")

print(input_date_list, input_time_list)

...and when executed:

python script.py << EOF
1-2-2023
11:59
EOF
(1, 2, 2023) (11, 59)

There is no (built-in) direct and easy way to specify the input's format in Python.

The input function in Python 3 ( raw_input in Python 2) will simply return the string that was typed to STDIN. Any parsing will be done manually on the string.

The input function in Python 2 ( eval(input()) in Python 3, which is not recommended ) did a very basic built-in parsing and would work for only a single element (ie equivalent to scanf("%d") for instance).

With some basic parsing you can get to not-so-complicated code that emulates scanf :

# scanf("%d-%d-%d")
input_date_list = [int(x) for x in input().split('-')]

# scanf("%d:%d")
input_time_list = [int(x) for x in input().split(':')]

For anything more complicated, more lines of code are needed. For example:

# scanf("%d,%f - %s")
nums, s = input().split(' - ')
i, f = nums.split(',')
i = int(i)
f = float(f)

in Python 2, you can use input() or raw_input()

s = input()     // gets int value        

k = raw_input()   // gets string value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM