formatting written data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    formatting written data

    hey everyone,

    if i have a list (or string) like this

    data = [1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14,15]

    and i wanted to write it to my file (lets say its called fn) so that i appears like this with 5 points per row

    1,2,3,4,5
    6,7,8,9,10
    11,12,13,14,15

    how much I got about that? I'm begginign to understand how to do it just as a print option in the python interpreter but, i'm a little confused on how to tell pyton to write it in a certain format.

    thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Patrick C
    hey everyone,

    if i have a list (or string) like this

    data = [1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14,15]

    and i wanted to write it to my file (lets say its called fn) so that i appears like this with 5 points per row

    1,2,3,4,5
    6,7,8,9,10
    11,12,13,14,15

    how much I got about that? I'm begginign to understand how to do it just as a print option in the python interpreter but, i'm a little confused on how to tell pyton to write it in a certain format.

    thanks
    You can use a list comprehension to format the list of integers for writing to disc.[code=Python]data = [1,2,3,4,5,6,7,8 ,9,10,11,12,13, 14,15]

    step = 5

    s = ''.join(['%s' % ([str(item)+',', str(item)+'\n'][not (i+1)%step or 0]) \
    for i, item in enumerate(data)
    ])
    print s

    >>> 1,2,3,4,5
    6,7,8,9,10
    11,12,13,14,15

    >>>[/code]Then you can use the file method write() to write the string to disc.

    Comment

    • Patrick C
      New Member
      • Apr 2007
      • 54

      #3
      exuse my ignorance, can anyone help me understand what this is doing. I'll write out the extent of my grasp...

      s = ''.join(['%s' % ([str(item)+',', str(item)+'\n'][not (i+1)%step or 0]) \
      for i, item in enumerate(data)
      ])



      ''.join(list) <--- makes a list into a string

      %s <--- refers to a latered reference string as in
      x = "pizza"
      "I want %s" %x
      >>> I want pizza

      str(item) + ',', str(item) + '\n'<--- would make 2 items into a string seperated by a comma and end it with a new line (but why only 2, when my desired output has 5 in a row??)

      not (i+1)%step or 0]) \ <----- THIS i dont know about

      for i, item in enumerate(data) <--- this sets what item is, that is, each element of data and tell it to look over each element in data and remember what place it was in the original list

      ]) <--- does that have to be a line lower?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by Patrick C
        exuse my ignorance, can anyone help me understand what this is doing. I'll write out the extent of my grasp...

        s = ''.join(['%s' % ([str(item)+',', str(item)+'\n'][not (i+1)%step or 0]) \
        for i, item in enumerate(data)
        ])



        ''.join(list) <--- makes a list into a string

        %s <--- refers to a latered reference string as in
        x = "pizza"
        "I want %s" %x
        >>> I want pizza

        str(item) + ',', str(item) + '\n'<--- would make 2 items into a string seperated by a comma and end it with a new line (but why only 2, when my desired output has 5 in a row??)

        not (i+1)%step or 0]) \ <----- THIS i dont know about

        for i, item in enumerate(data) <--- this sets what item is, that is, each element of data and tell it to look over each element in data and remember what place it was in the original list

        ]) <--- does that have to be a line lower?
        Following is another way to achieve the same thing.[code=Python]output = []
        for i, num in enumerate(data) :
        if not (i+1) % step:
        output.append(s tr(num)+'\n')
        else:
        output.append(s tr(num)+',')
        s = ''.join(output)
        print s

        >>> 1,2,3,4,5
        6,7,8,9,10
        11,12,13,14,15
        [/code]I thing you have a pretty good grasp of what is taking place. The '])' does not need to be on the next line. I do that sometimes when the line is over 80 characters in length. This snippet str(item) + ',', str(item) + '\n' is actually a list and the proper string is passed to the format operator using a decision slice. Interactive:[code=Python]>>> i = 1
        >>> [not (i+1)%step or 0]
        [0]
        >>> i = 4
        >>> [not (i+1)%step or 0]
        [True]
        >>> [str(item)+',', str(item)+'\n'][not (i+1)%step or 0]
        '15\n'
        >>> i = 1
        >>> [str(item)+',', str(item)+'\n'][not (i+1)%step or 0]
        '15,'
        >>> [/code]

        Comment

        Working...