Formatted input function

C

candide

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 ?
 
C

Chris Rebert

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
 
G

Günther Dietrich

candide said:
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
 
C

Chris Rebert

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
 
C

candide

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 :
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top