Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Formatted input function

Reply
Thread Tools

Formatted input function

 
 
candide
Guest
Posts: n/a
 
      09-05-2009
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 ?
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      09-05-2009
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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      09-05-2009
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

 
Reply With Quote
 
Günther Dietrich
Guest
Posts: n/a
 
      09-05-2009
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
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      09-05-2009
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
 
Reply With Quote
 
candide
Guest
Posts: n/a
 
      09-05-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
formatted input/output question dkk C Programming 2 04-12-2006 05:27 PM
Reporting formatted input operations that fail Jason Heyes C++ 0 06-24-2005 06:19 AM
J2SE 1.5 Formatted Input Question Matt Java 3 10-19-2004 08:07 AM
Formatted input interpretation TheDD C++ 7 05-15-2004 10:19 PM
From unformatted to formatted input TheDD C++ 4 05-13-2004 09:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57