[Tutor] Read and write list on I file

2013-01-26 Thread jarod...@libero.it
HI there!!!
I have a file like this:

12345-2 ppp
12389-4

i
...

I want to read  this file and organize in different way: The second number
present  after - mean the times are ripetuted the elements..so in the 
example
In the end I want to  have this result

12345-1 ppp
12345-2 ppp
12389-1

i
12389-2

i
12389-3

i
12389-4

i
Someone have a suggestion how to do this simple task.. I write some code but
don't do all the think I want.

import re

f = open(file.txt,r)

for item in f:
 if re.match(r'['0-9]',item):
  line=iteme.strip.split(\t)
  num = line[0].split(-)
  key = num[0]
  vl = in(num[1])
  for i in range(1,vl):
 

   else:
   print all line





thanks in advance for any help!

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read and write list on I file

2013-01-26 Thread Steven D'Aprano

On 26/01/13 20:21, jarod...@libero.it wrote:

(edited slightly to make it more clear)


HI there!!!
I have a file like this:

12345-2 p
12389-4 i

I want to read  this file and organize in different way: The second number
present  after - mean the times are ripetuted the elements..so in the
example In the end I want to  have this result

12345-1 p
12345-2 p
12389-1 i
12389-2i
12389-3i
12389-4i

Someone have a suggestion how to do this simple task..


Untested, but this should work. Adding error checking is left up to you.


f = open('file.txt', 'r')
# each line looks like:
# d-d ccc...
# where d is a digit and c is any non-space character.

for line in f:
head, tail = line.split(' ')
head, num = head.split('-')
num = int(num)
for i in range(1, num+1):
print %s-%s %s % (head, i, tail)

f.close()



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor