scanf in python [英] scanf in python

查看:123
本文介绍了scanf in python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我经常需要解析包含混合字符的字符串,

整数和浮点数,C语言的scanf函数非常这个目的很实用



我一直在寻找这样的功能,我很惊讶

发现它有早在2001年就已经讨论过但从未实施过b $ b。建议的方法似乎是使用拆分然后使用正则表达式,然后使用atoi和atof。两种方法

似乎都比scanf更自然,更麻烦。如果

python已经有一个行为类似于printf的%字符串运算符,为什么不用

实现%%或<<字符串运算符的行为类似于scanf,使用

可能类似于以下内容:


a,b,c ="%d%f%5c" %%" 1 2.0 abcde"




a,b,c ="%d%f%5c" << 1 2.0 abcde


%%更接近%运营商


<<对我来说似乎更直观


这种方法中的任何一种在我看来都比我简单得多:


lst =" 1 2; 0 abcde" .split()

a = int(lst [0])

b = float(lst [1])

c = lst [2] <当使用正则表达式解析这样简单的输入时,
甚至更糟。


我喜欢python,因为它简洁易读,我真的很喜欢想想

它可以使用这样的运营商。


我知道这已经讨论了很多次,但以前都讨论了

我找到的线程似乎已经死了,我想进一步邀请

调查这个话题。


干杯,

$ b $bAndréM。Descombes

解决方案

AMD schrieb:


您好,


我经常需要解析包含混合字符的字符串,

整数和浮点数,C语言scanf函数是非常实用

这个目的。

我一直在寻找这样的功能,我很惊讶

找到它早在2001年就已经讨论过,但从未实施过b $ b。建议的方法似乎是使用拆分然后使用正则表达式,然后使用atoi和atof。两种方法

似乎都比scanf更自然,更麻烦。如果

python已经有一个行为类似于printf的%字符串运算符,为什么不用

实现%%或<<字符串运算符的行为类似于scanf,使用

可能类似于以下内容:


a,b,c ="%d%f%5c" %%" 1 2.0 abcde"




a,b,c ="%d%f%5c" << 1 2.0 abcde


%%更接近%运营商


<<对我来说似乎更直观


这种方法中的任何一种在我看来都比我简单得多:


lst =" 1 2; 0 abcde" .split()

a = int(lst [0])

b = float(lst [1])

c = lst [2] <当使用正则表达式解析这样简单的输入时,
甚至更糟。


我喜欢python,因为它简洁易读,我真的很喜欢想想

它可以使用这样的运营商。


我知道这已经讨论了很多次,但以前都讨论了

我发现的线程似乎已经死了,我想再邀请一下这个话题的调查。



我很确定python不会为此增加额外的运算符。

然而你可以自由创建一个scanf - 作为第三方模块的实现。


恕我直言,该方法的可用性非常有限。首先,

需要捕获多个输入令牌*非常*很少 - 几乎

所有命令行工具我知道确实需要交互式用户输入

(就像linux内核配置工具一样)通过提供

逐行值输入(包括默认值,你不能做的事情)

与你的方法一起),甚至以对话为中心的价值输入与诅咒。


所以 - 我怀疑你会收集很多动力。祝你好运。

Diez


>


我很确定python不会为此增加额外的运算符。

然而你可以自由创建一个scanf - 作为第三方模块的实现。


恕我直言,该方法的可用性非常有限。首先,

需要捕获多个输入令牌*非常*很少 - 几乎

所有命令行工具我知道确实需要交互式用户输入

(就像linux内核配置工具一样)通过提供

逐行值输入(包括默认值,你不能做的事情)

与你的方法一起),甚至以对话为中心的价值输入与诅咒。


所以 - 我怀疑你会收集很多动力。祝你好运。


Diez



实际上它很常见,它用于处理不是用于
读取参数。您可以在需要阅读简单的

csv文件或固定格式文件时使用它,该文件包含许多行,每行有几个

字段。

该方法的优点是它将字段的解析和转换结合到一个操作中。

使用简单格式化字符串的另一个优点是它允许

可以轻松翻译这些行,就像你使用%

运算符输出一样。我不明白为什么python可以有一个运算符来输出

,但它不能有一个用于输入,它只是不对称。

我不明白为什么你不能用这种方法逐行输入值,

只需在你的%s或%d之间加上\ n。

这种方法非常通用,比正则表达式简单得多

加之后的转换。

$ b $bAndré


< blockquote> AMD写道:


你好,


我经常需要解析包含混合字符的字符串,

整数和浮点数,C语言scanf函数对于

非常实用。

我一直在寻找这样的功能和我很惊讶

发现它早在2001年就被讨论过但从未实现过



第二个Google点击是scanf的纯Python实现。

http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/
< br $> b $ b -

Robert Kern


我开始相信整个世界都是一个谜,一个无害的谜团<由于我们疯狂地试图将它解释为一个潜在的真相,因为它是可怕的。

- Umberto Eco


Hello,

I often need to parse strings which contain a mix of characters,
integers and floats, the C-language scanf function is very practical for
this purpose.
I''ve been looking for such a feature and I have been quite surprised to
find that it has been discussed as far back as 2001 but never
implemented. The recommended approach seems to be to use split and then
atoi or atof or to use regex and then atoi and atof. Both approaches
seem to be a lot less natural and much more cumbersome than scanf. If
python already has a % string operator that behaves like printf, why not
implement either a %% or << string operator to behave like scanf, use
could be like the followng:

a, b, c = "%d %f %5c" %% "1 2.0 abcde"

or

a, b, c = "%d %f %5c" << "1 2.0 abcde"

%% is closer to the % operator

<< seems more intuitive to me

either of this methods seems to me much simpler than:

lst = "1 2;0 abcde".split()
a = int(lst[0])
b = float(lst[1])
c = lst[2]

or even worse when using regular expressions to parse such simple input.

I like python because it is concise and easy to read and I really think
it could use such an operator.

I know this has been discussed before and many times, but all previous
threads I found seem to be dead and I would like to invite further
investigation of this topic.

Cheers,

André M. Descombes

解决方案

AMD schrieb:

Hello,

I often need to parse strings which contain a mix of characters,
integers and floats, the C-language scanf function is very practical for
this purpose.
I''ve been looking for such a feature and I have been quite surprised to
find that it has been discussed as far back as 2001 but never
implemented. The recommended approach seems to be to use split and then
atoi or atof or to use regex and then atoi and atof. Both approaches
seem to be a lot less natural and much more cumbersome than scanf. If
python already has a % string operator that behaves like printf, why not
implement either a %% or << string operator to behave like scanf, use
could be like the followng:

a, b, c = "%d %f %5c" %% "1 2.0 abcde"

or

a, b, c = "%d %f %5c" << "1 2.0 abcde"

%% is closer to the % operator

<< seems more intuitive to me

either of this methods seems to me much simpler than:

lst = "1 2;0 abcde".split()
a = int(lst[0])
b = float(lst[1])
c = lst[2]

or even worse when using regular expressions to parse such simple input.

I like python because it is concise and easy to read and I really think
it could use such an operator.

I know this has been discussed before and many times, but all previous
threads I found seem to be dead and I would like to invite further
investigation of this topic.

I''m pretty certain python won''t grow an additional operator for this.
Yet you are free to create a scanf-implementation as 3rd-party-module.

IMHO the usability of the approach is very limited though. First of all,
the need to capture more than one input token is *very* seldom - nearly
all commandline-tools I know that do require interactive user-input
(like the linux kernel config tool) do so by providing either
line-by-line value entry (including defaults, something you can''t do
with your approach), or even dialog-centric value entry with curses.

So - I doubt you will gather much momentum on this. Good luck though.
Diez


>

I''m pretty certain python won''t grow an additional operator for this.
Yet you are free to create a scanf-implementation as 3rd-party-module.

IMHO the usability of the approach is very limited though. First of all,
the need to capture more than one input token is *very* seldom - nearly
all commandline-tools I know that do require interactive user-input
(like the linux kernel config tool) do so by providing either
line-by-line value entry (including defaults, something you can''t do
with your approach), or even dialog-centric value entry with curses.

So - I doubt you will gather much momentum on this. Good luck though.
Diez

Actually it is quite common, it is used for processing of files not for
reading parameters. You can use it whenever you need to read a simple
csv file or fixed format file which contains many lines with several
fields per line.
The advantage of the approach is that it combines the parsing and
conversion of the fields into one operation.
Another advantage of using simple formatting strings is that it allows
for easy translation of these lines, just like you have with the %
operator for output. I don''t see why python can have an operator for
output but it can''t have one for input, it''s just not symmetrical.
I don′t see why you can''t use this method for line-by-line value entry,
just add \n between your %s or %d.
The method is quite versatile and much simpler than regular expressions
plus conversion afterwards.

André


AMD wrote:

Hello,

I often need to parse strings which contain a mix of characters,
integers and floats, the C-language scanf function is very practical for
this purpose.
I''ve been looking for such a feature and I have been quite surprised to
find that it has been discussed as far back as 2001 but never
implemented.

The second Google hit is a pure Python implementation of scanf.

http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


这篇关于scanf in python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆