简体   繁体   English

Python function 用于从用户获取格式化输入,类似于“C”中的 scanf()

[英]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.我想知道 Python 中是否有一个 function 从用户那里获取格式化输入,类似于使用scanf()和格式化字符串(例如%d%lf等)从“C”中获取用户输入。

Hypothetical example in which scanf() returns a list : scanf()返回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() . input() For example this could be used as:例如,这可以用作:

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

For use in Python 2, this would be raw_input() .对于在 Python 2 中使用,这将是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.标准库中没有直接的 scanf(3) 等价物。 The documentation for the re module suggests itself as a replacement, providing this explanation : re模块的文档建议自己作为替代品,提供以下解释

Python does not currently have an equivalent to scanf(). Python 目前没有等同于 scanf() 的函数。 Regular expressions are generally more powerful, though also more verbose, than scanf() format strings.正则表达式通常比 scanf() 格式字符串更强大,但也更冗长。 The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions.下表提供了 scanf() 格式标记和正则表达式之间的一些或多或少的等效映射。

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). Pypi 上的scanf 模块提供了一个更类似于 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.在 Python 中没有(内置的)直接和简单的方法来指定输入的格式。

The input function in Python 3 ( raw_input in Python 2) will simply return the string that was typed to STDIN. Python 3 中的input function(Python 2 中的raw_input )将简单地返回输入到 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). Python 2中的input function( 不推荐Python 3 中的eval(input()) )进行了非常基本的内置解析,并且仅适用于单个元素(即相当于scanf("%d") for实例)。

With some basic parsing you can get to not-so-complicated code that emulates scanf :通过一些基本的解析,您可以获得模拟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()在 Python 2 中,您可以使用input()raw_input()

s = input()     // gets int value        

k = raw_input()   // gets string value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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