473,396 Members | 1,998 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,396 software developers and data experts.

passing an int/float via f.write()

hey guys is it possible 2 do so??? as in like i want 2 write numbers from 1 to 1000 in a file f.txt say.... using the while loop im stuck... as the indexing variable for incrementing the numbers to 1000 ITSELF gets printed if i use '' or "" or '\j' or "\j"...

also is there an easy way of converting this c++ code 2 a python code [without suing swig].... its basically arrays....
Expand|Select|Wrap|Line Numbers
  1. float a[6][6];   
  2. for(int i=0;i<5;i++)
  3. {
  4. a[2][2]=i;
  5. a[4][3]=-i;
  6. }
I can easily substitue tht for loop in c++ with the standard while loop in python... im not getting the arrays part... any help wud b appreciated
Jul 8 '07 #1
3 2380
bartonc
6,596 Expert 4TB
hey guys is it possible 2 do so??? as in like i want 2 write numbers from 1 to 1000 in a file f.txt say.... using the while loop im stuck... as the indexing variable for incrementing the numbers to 1000 ITSELF gets printed if i use '' or "" or '\j' or "\j"...

also is there an easy way of converting this c++ code 2 a python code [without suing swig].... its basically arrays....
Expand|Select|Wrap|Line Numbers
  1. float a[6][6];   
  2. for(int i=0;i<5;i++)
  3. {
  4. a[2][2]=i;
  5. a[4][3]=-i;
  6. }
I can easily substitue tht for loop in c++ with the standard while loop in python... im not getting the arrays part... any help wud b appreciated
Python has string formatting much like C's:
Expand|Select|Wrap|Line Numbers
  1. floatStr = "%.3f" % 6.99999999
  2. print floatStr, type(floatStr)
If you don't care about the formatting, just call the built-in str() method:
Expand|Select|Wrap|Line Numbers
  1. aFloat = 4.9
  2. print numberStr = str(aFloat)
  3. print numberStr , type(numberStr )
for data storage in a text file, I often use a strings join() method:
Expand|Select|Wrap|Line Numbers
  1. "\t".join(["%.3f" %aFloat, str(anInt)])
For true matrix work, you'll want to add SciPy to your installation.
Constructors for scipy arrays work much the same as in c.


It's really time for you to start using code tags in your posts. Instructions are on the right hand side of the page while posting.
Jul 8 '07 #2
bvdet
2,851 Expert Mod 2GB
hey guys is it possible 2 do so??? as in like i want 2 write numbers from 1 to 1000 in a file f.txt say.... using the while loop im stuck... as the indexing variable for incrementing the numbers to 1000 ITSELF gets printed if i use '' or "" or '\j' or "\j"...

also is there an easy way of converting this c++ code 2 a python code [without suing swig].... its basically arrays....
Expand|Select|Wrap|Line Numbers
  1. float a[6][6];   
  2. for(int i=0;i<5;i++)
  3. {
  4. a[2][2]=i;
  5. a[4][3]=-i;
  6. }
I can easily substitue tht for loop in c++ with the standard while loop in python... im not getting the arrays part... any help wud b appreciated
This will write 1-1000 to a file, ten numbers on each line:
Expand|Select|Wrap|Line Numbers
  1. f = open('f.txt', 'w')
  2. f.write('\n'.join([' '.join([str(i+j) for i in range(0,10)]) for j in range(1,1001, 10)]))
  3. f.close()
Jul 8 '07 #3
bartonc
6,596 Expert 4TB
hey guys is it possible 2 do so??? as in like i want 2 write numbers from 1 to 1000 in a file f.txt say.... using the while loop im stuck... as the indexing variable for incrementing the numbers to 1000 ITSELF gets printed if i use '' or "" or '\j' or "\j"...

also is there an easy way of converting this c++ code 2 a python code [without suing swig].... its basically arrays....
Expand|Select|Wrap|Line Numbers
  1. float a[6][6];   
  2. for(int i=0;i<5;i++)
  3. {
  4. a[2][2]=i;
  5. a[4][3]=-i;
  6. }
I can easily substitue tht for loop in c++ with the standard while loop in python... im not getting the arrays part... any help wud b appreciated
Here is a recent discussion on matrices.
Jul 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: NM | last post by:
Hello all, I am supposed to do some mixed programming with c++ and fortran. I was succeeful in exchanging the 2D arrays from fortran to c++ and the other way, but was unable to that same with...
15
by: Chris Readle | last post by:
Hi all, Somewhat new to C and I'm getting the following error from my latest code. Here's the warning I'm getting: chris_readle_project3_assignment3.c: In function `main':...
3
by: ehabaziz2001 | last post by:
I know one of the pointer benefit is that we can return more than one value from a function . The down program has an error that I can not discover . That is the call of the function : The Call:...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
8
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I'm trying to pass values of different data-types to a web-service. I thought it would be easier to box these values and pass them as a System.object parameter, like public void...
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
3
by: PeterOut | last post by:
Say I have a function like this. int Func(float **fppArg); and I have a variable defined thus. float faa2DArray; How would I pass faa2DArray to Func()?
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
1
by: Fizzics | last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.