-1

I know this is an easy one but I am brand new to programming and any help would be greatly appreciated.

I have a text file that has a bunch of numbers in it (one per line). I need to open the file and split the number into three. If the number is "123456" i need to split it into "14","25","36" in other words I need it to go (x1x2), (y1y2), (z1,z2) for a number x1y1z1x2y2z2. For odd numbers I want to add a zero to the last group to even it out. Thanks for the help, I am hopeless at programming.

English Grad
  • 1,365
  • 5
  • 21
  • 40
  • 1
    What exactly are you asking for? Someone to write this for you? – Andy Jul 23 '11 at 23:54
  • I don't want someone to write it for me I just do not know the best way to handle it. The gentlemen below said lists but I do not know if that is the best way and I can open and read files but I do not know how to make list from a file I read. I can make a list from scratch but that doesn't help me. I am trying to learn but as a complete beginner I am lost – English Grad Jul 24 '11 at 00:10
  • This has been asked before here: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python – dave Jul 24 '11 at 00:13
  • Could you clarify what you mean by "add a zero to the last group to even it out"? – kjakeb Jul 24 '11 at 00:14
  • Yes. I do not know how long the line is. if it has 5 numbers in it then I will only have one number in the third group. If any number has the form X1Y1Z1...XnYnZn, I want to make three groups X,Y,Z then repeat for the next line – English Grad Jul 24 '11 at 00:28

3 Answers3

2

One simple suggestion. Covert your number to a list and operate on the elements of the list.

>>> list("123456")
['1', '2', '3', '4', '5', '6']
>>> 

Now, it would much easier for you to handle. If not, then you should perhaps start with some Python tutorials.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
1

This should satisfy your example:

s = "123456"
ss = [s[i::3] for i in range(3)]
ss
> ['14', '25', '36']

To make sure the strings are equal length, you can either pad the original string:

s = s.ljust((len(s)+2)//3 * 3, '0')

or do:

l = (len(s)+2)//3
ss = [s[i::3].ljust(l, '0') for i in range(3)]
viraptor
  • 33,322
  • 10
  • 107
  • 191
0

So because you're slicing into thirds, the problem isn't odd numbers, but is rather numbers not divisible by 3. Here's a function that accepts a string and returns a tuple of slices of that string.

def triplet(s):
    extra_zeros = (3 - len(s)) % 3
    s += '0' * extra_zeros
    return (s[::3], s[1::3], s[2::3])

Here's a demonstration of its behavior:

>>> triplet('123456')
('14', '25', '36')
>>> triplet('1234567')
('147', '250', '360')
>>> triplet('12345678')
('147', '258', '360')
>>> triplet('123456789')
('147', '258', '369')
senderle
  • 145,869
  • 36
  • 209
  • 233
  • Senderle, this is almost working except it is including the endline from the file. Here is some output: ('1551234914', '221985214\n', '1294110550') The Problem is the \n should not be there – English Grad Jul 24 '11 at 04:32
  • Ah, you just need to `strip` the line first. Do it right after you read it from the file. Something like `lines = [line.strip() for line in in_file]`. – senderle Jul 24 '11 at 14:08