passing an int/float via f.write()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vajratkarviraj
    New Member
    • Jun 2007
    • 15

    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....
    [CODE=c]
    float a[6][6];
    for(int i=0;i<5;i++)
    {
    a[2][2]=i;
    a[4][3]=-i;
    }[/CODE]

    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
    Last edited by bartonc; Jul 8 '07, 08:36 PM. Reason: Added [CODE=c][CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by vajratkarviraj
    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....
    [CODE=c]
    float a[6][6];
    for(int i=0;i<5;i++)
    {
    a[2][2]=i;
    a[4][3]=-i;
    }[/CODE]

    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:[CODE=python]
    floatStr = "%.3f" % 6.99999999
    print floatStr, type(floatStr)[/CODE]

    If you don't care about the formatting, just call the built-in str() method:[CODE=python]aFloat = 4.9
    print numberStr = str(aFloat)
    print numberStr , type(numberStr )[/CODE]
    for data storage in a text file, I often use a strings join() method:[CODE=python]"\t".join(["%.3f" %aFloat, str(anInt)])[/CODE]

    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.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by vajratkarviraj
      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....
      [CODE=c]
      float a[6][6];
      for(int i=0;i<5;i++)
      {
      a[2][2]=i;
      a[4][3]=-i;
      }[/CODE]

      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:[code=Python]f = open('f.txt', 'w')
      f.write('\n'.jo in([' '.join([str(i+j) for i in range(0,10)]) for j in range(1,1001, 10)]))
      f.close()[/code]

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by vajratkarviraj
        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....
        [CODE=c]
        float a[6][6];
        for(int i=0;i<5;i++)
        {
        a[2][2]=i;
        a[4][3]=-i;
        }[/CODE]

        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.

        Comment

        Working...