Formatted input function

Discussion in 'Python' started by candide, Sep 5, 2009.

  1. candide

    candide Guest

    I have a text file and the first line provides the best score of a game. This
    line has the following format :

    Best score : 42

    In order to update the score, I try to retrieve the score value.

    In C, we would manage this with the following statement :

    fscanf(foo_file, "Best score : %d", &score);

    Does Python provide an equivalent ?
    candide, Sep 5, 2009
    #1
    1. Advertising

  2. candide

    Chris Rebert Guest

    On Sat, Sep 5, 2009 at 3:13 AM, candide<> wrote:
    > I have a text file and the first line provides the best score of a game. This
    > line has  the following format :
    >
    > Best score : 42
    >
    > In order to update the score, I try to retrieve the score value.
    >
    > In C, we would manage this with the following statement :
    >
    > fscanf(foo_file, "Best score : %d", &score);
    >
    > Does Python provide an equivalent ?


    Not an exact one, but it's still easily accomplished:

    score = int(foo_file.read().split(":").strip())

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Sep 5, 2009
    #2
    1. Advertising

  3. candide

    Terry Reedy Guest

    Chris Rebert wrote:
    > On Sat, Sep 5, 2009 at 3:13 AM, candide<> wrote:
    >> I have a text file and the first line provides the best score of a game. This
    >> line has the following format :
    >>
    >> Best score : 42
    >>
    >> In order to update the score, I try to retrieve the score value.
    >>
    >> In C, we would manage this with the following statement :
    >>
    >> fscanf(foo_file, "Best score : %d", &score);
    >>
    >> Does Python provide an equivalent ?

    >
    > Not an exact one, but it's still easily accomplished:
    >
    > score = int(foo_file.read().split(":").strip())


    One must, of course, select the second item from the list returned by
    ..split. The .strip() seems not to be needed, at least in 3.1, as int()
    ignores whitespace.

    >>> s='Best score : 42\n'
    >>> int(s.split(':')[1])

    42
    Terry Reedy, Sep 5, 2009
    #3
  4. candide <> wrote:

    >In C, we would manage this with the following statement :
    >
    >fscanf(foo_file, "Best score : %d", &score);
    >
    >Does Python provide an equivalent ?


    RTFM! :)

    In the python 2.5 manual: Chapter 4.2.6 (search pattern 'scanf' on the
    index tab of python25.chm)

    There are some scanf-patterns with corresponding regular expressions
    listed.

    Works quite nice.



    Best regards,

    Günther
    Günther Dietrich, Sep 5, 2009
    #4
  5. candide

    Chris Rebert Guest

    On Sat, Sep 5, 2009 at 8:00 AM, Terry Reedy<> wrote:
    > Chris Rebert wrote:
    >>
    >> On Sat, Sep 5, 2009 at 3:13 AM, candide<> wrote:
    >>>
    >>> I have a text file and the first line provides the best score of a game..
    >>> This
    >>> line has  the following format :
    >>>
    >>> Best score : 42
    >>>
    >>> In order to update the score, I try to retrieve the score value.
    >>>
    >>> In C, we would manage this with the following statement :
    >>>
    >>> fscanf(foo_file, "Best score : %d", &score);
    >>>
    >>> Does Python provide an equivalent ?

    >>
    >> Not an exact one, but it's still easily accomplished:
    >>
    >> score = int(foo_file.read().split(":").strip())

    >
    > One must, of course, select the second item from the list returned by
    > .split.


    (whacks forehead)

    One should not answer threads at 3AM local time without
    double-checking the solution...

    - Chris
    Chris Rebert, Sep 5, 2009
    #5
  6. candide

    candide Guest

    Günther Dietrich a écrit :

    >
    > RTFM! :)
    >
    > In the python 2.5 manual: Chapter 4.2.6 (search pattern 'scanf' on the
    > index tab of python25.chm)



    Correct !! For once ;) the Manual gives an inambiguous answer :

    ----------------------
    Simulating scanf() .-- Python does not currently have an equivalent to scanf().
    Regular expressions are generally more powerful, though also more verbose, than
    scanf() format strings.
    ----------------------

    I'm realizing that using re module is worthwhile but I never tried to learn it.
    candide, Sep 5, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    433
    Eric Reitmaier
    Oct 19, 2004
  2. TheDD
    Replies:
    4
    Views:
    512
    John Harrison
    May 13, 2004
  3. TheDD
    Replies:
    7
    Views:
    379
    TheDD
    May 15, 2004
  4. dkk

    formatted input/output question

    dkk, Apr 10, 2006, in forum: C Programming
    Replies:
    2
    Views:
    314
    Herbert Rosenau
    Apr 12, 2006
  5. Jason Heyes
    Replies:
    0
    Views:
    273
    Jason Heyes
    Jun 24, 2005
Loading...

Share This Page