python equivalent for fputc

P

Putty

I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.

So what kind of call can I make to do it in Python?
 
B

bearophileHUGS

Putty said:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.
So what kind of call can I make to do it in Python?

There may be some ways. Here is the data:
from random import randint
vals = [randint(0, 255) for i in xrange(10)]

This is probably the most common way:
print "".join(chr(c) for c in vals)

If you need to access the chars one after the other, this may be a
(quite slower, but less memory consuming) alternative:

import sys
write = sys.stdout.write
for c in vals:
write(chr(c))
print

In some cases a solution like this one can be useful too:

import array
print array.array("B", vals).tostring()

Bye,
bearophile
 
S

Sybren Stuvel

Putty enlightened us with:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.

So what kind of call can I make to do it in Python?

somefile.write('x')

To write 'x' to somefile.

Sybren
 
J

John Machin

Putty said:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.

C is a low-level language -- a character *is* a number :)
So what kind of call can I make to do it in Python?

*Guessing* that you mean e.g. fputc(97, f) writes the character 'a' to
the file whose handle is f ...

In Python the more-or-less literal translation (ignoring the fputc
return value) would be f.write(chr(97))

Note:
|>>> ord('a')
97
|>>> chr(97)
'a'

HTH -- if not, show us (relevant parts of) the actual source that you
are porting.

Cheers,
John
 
F

Fredrik Lundh

Putty said:
I'm porting a program a friend wrote in C over to Python and I've run
into a little hang-up. The C program writes characters out to a file.
I'm 99% sure that a conversion is going on here as well. I know for a
fact that it's taking a number and turning it into a character.

that's what fputc does, of course -- it takes an integer and writes
it as a character to the given stream.

f = fopen(filename, "w");
fputc(c, f);
fclose(f)

becomes

f = open(filename, "w")
f.write(chr(value))
f.close()

</F>
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top