473,394 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

python equivalent for fputc

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?

Aug 31 '06 #1
4 3091
Putty wrote:
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

Aug 31 '06 #2

Putty wrote:
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

Aug 31 '06 #3
Thanks, John. That's exactly what I was looking for!

John Machin wrote:
Putty wrote:
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
Aug 31 '06 #4
Putty wrote:
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>

Aug 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
4
by: Summu82 | last post by:
Hi i have to write some data in a binary file fputc(0x22,f2); this works fine also int data =0x22; fputc(0x22,f2);
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
20
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
3
by: =?iso-8859-9?B?RGlu52F5IEFr5/ZyZW4=?= | last post by:
When I execute the following code #include <stdio.h> #include <stdlib.h> int main(void) { FILE *bmp; bmp = fopen("deneme.bmp","w"); fputc(10,bmp);
3
by: ocli5568 | last post by:
My program is an 'attempt' at steganography, and i've written what i hope to be correct for the most part, although i'm having a lot of trouble with copying the picture input to picture output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.