1

If I've an array:

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

How can I save it on the following format? 5 values + coma + other 5 values + coma, etc.

12345, 678910, 1112131415

What I've tried and it's not giving me the desired solution:

f = open('file.txt')
for item in stuff:
  for i in xrange(5):
      f.write("%s", item)
  f.write(", ")
f.close()

Thanks in advance.

JoeBilly
  • 3,057
  • 29
  • 35
Avión
  • 7,963
  • 11
  • 64
  • 105

5 Answers5

3

Try this way:

>>> s
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> n
5
>>> [int(''.join(str(x) for x in s[i*n: i*n + n])) for i in range(len(s)/n)]
[12345, 678910, 1112131415]

just a suggestion not completely tested code

Edit: To write in a file do like:

>>> L = [''.join(str(x) for x in s[i*n: i*n + n]) for i in range(len(s)/n)]
>>> with open("file", 'w') as f:
...  for v in L:
...   f.write(v + ', ')
... 

I have not cast to int when I write in a file.

Updated: improved code
For loss less split a list of any length you can do ceiling division. I have improved the code and added below:

def split_list(L, n):
  """split successive n-sized groups from L as a list 
  """
  from math import ceil
  parts = int(ceil(len(L)/float(n))) # ceilng division 5/2 ==> 3
  return [int(''.join(str(_) for _ in L[i*n: i*n + n])) for i in range(parts)]

Some test-cases

# test cases
n = 5
s = [1, 2, 3 ]
print split_list(s, 5) # output [123]
s = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
print split_list(s, 5) # output [12345, 678910, 111213]
s = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
print split_list(s, 5) # output [12345, 678910, 1112131415]
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
1

According to my comment, here is some ugly quick example:

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

with open('file.txt') as f:        
    f.write(', '.join([''.join((str(y) for y in x)) for x in chunks(stuff, 5)]))

@Grijesh Chauhan answer also good

Andrii Rusanov
  • 4,405
  • 2
  • 34
  • 54
1

the simplest way, use a counter

f = open("file.txt")
cnt = 0
for item in studff:
  f.write("%s", item)
  cnt = cnt + 1
  if cnt % 5 == 0:
    f.write(", ")
f.close()
Zhou Tai
  • 214
  • 1
  • 10
1

As your needs are simple. no need to split arrays, etc. Specially when you need to iterate to write them anyway, use enumreate.

Here's a Python3 code printing to stdout, you can change it easily for file output.

for i, x in enumerate(stuff, 1):
    print(x, end="")
    if i % 5 == 0:
        print(",", end="")
mkriheli
  • 1,788
  • 10
  • 18
1

If you also need a separator between your 5 values then use the following:

stuff = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
n = 5
main_separator = ','
secondary_separator = '-'
#secondary_separator = '-' # <- when you do not need a secondary separator between the values
result = main_separator.join([secondary_separator.join([str(i) for i in stuff[i*n: i*n + n]]) for i in xrange(len(stuff)/n)])
print result

This produces following output

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

Which you can print to a file simply by:

whith open('filename', 'w)
h.write(result)
h.close()
wolfrevo
  • 6,651
  • 2
  • 26
  • 38